Cannot find package "." in .../vendor/github.com/godror/godror - go

I'm new to golang. I'm currently trying to use the godror driver to read from an Oracle db. I ran go get github.com/godror/godror in my project's root directory and am importing the library like so:
_ "github.com/godror/godror"
But, I'm getting the error
cannot find package "." in:
/test_repo/vendor/github.com/godror/godror"
I believe my PATH is set up properly, as the "go" command properly returns the expected "Go is a tool for managing Go source code..." response.

I can't exactly replicate your issue nor have I seen such a weird error - but regardless, if you were following the current go modules pattern you wouldn't have this issue to begin with.
You shouldn't run go get anymore to download modules to use for your programs. Instead, in the root directory of every go project, you'll run go mod init [modulename], which will create a go.mod file for you. After running go mod tidy, it will download all the dependencies and generate a go.sum file containing the dependency hashes for you as well. Next, running go build will generate a binary that you can run. At this point, if you make changes to any source file(s), running go build every subsequent time afterwards will make a new, updated binary in the same directory.

Related

VS Code showing me "Error loading workspace: found module "main.go" twice in the workspace"

I am using the primary GO extension.
I use VS code a lot, now I'm learning GO lang.
And when I open VS Code every time I'm getting this:
Error loading workspace: found module "main.go" twice in the workspace.
While running the code it's giving the right output.
I don't know how to fix this.
Anybody, help me with this error.
It would be better to open in VSCode only one folder with its own go.mod project.
A workspace with multiple go.mod/project should be supported with 1.18
The go command now supports a "Workspace" mode.
If a go.work file is found in the working directory or a parent directory, or one is specified using the -workfile flag, it will put the go command into workspace mode.
In workspace mode, the go.work file will be used to determine the set of main modules used as the roots for module resolution, instead of using the normally-found go.mod file to specify the single main module.
As described in "How to make VScode Go work in a Multi-Module Repo" from Varun Kumar, this used to work:
If you want to work with all the nested modules in a single workspace, there is an opt-in module feature that allows to work with multiple modules without creating workspace folders for each module. Set this in your settings -
"build.experimentalWorkspaceModule": true
But as per september 2022 is deprecated.
See more at gopls documentation "Setting up your workspace".

Cannot build github.com/jonpchin/gochess - "working directory is not part of a module"

I unwrapped the project and from that project i did go get. Then I received the following errors.
C:\Users\Downloads\gochess-master>go install main.go
main.go:14:2: no required module provides package github.com/dchest/captcha: working directory is not part of a module
main.go:15:2: no required module provides package github.com/go-sql-driver/mysql: working directory is not part of a module
main.go:17:2: no required module provides package github.com/jonpchin/gochess/goforum: working directory is not part of a module
main.go:18:2: no required module provides package github.com/jonpchin/gochess/gostuff: working directory is not part of a module
main.go:20:2: no required module provides package golang.org/x/net/websocket: working directory is not part of a module
Then I went and tried doing go get each of the packages. I try doing the go install main.go and got same error.
then I tried including the repo inside $GOPATH/src/github.com/jonpchin/gochess and tried doing go get from there. I got the same errors.
github.com/jonpchin/gochess is a couple of years old and has not been updated to use go modules. I suspect you are using Go 1.16 under which "Module-aware mode is enabled by default, regardless of whether a go.mod file is present in the current working directory or a parent directory". There are a few ways to address this but given that there is a plan to "drop support for GOPATH mode in Go 1.17" the simplest approach might be to download the code and setup modules yourself. The following process works for me (well I get prompted for a mysql password):
git clone https://github.com/jonpchin/gochess
cd gochess
go mod init github.com/jonpchin/gochess
go get
go build
.\gochess.exe

Can't run test function in GoLand: cannot find package "."

I have used VS Code as my code editor for building service using Go for almost one year. Then, I tried to switch to GoLand. But, when I tried run a test function there is an error: cannot find package "." What is the problem?
Note: I use go module as go dependency management tool. When I use dep (in another project), there is no error when running a test function. My project is in GOPATH.
Please make sure there's a valid go.mod file defined at the ROOT of your project i.e. in $GOPATH/src/<Project-name>.
If you don't have, you can create one using go mod init command using the shell. More information on the same - https://github.com/golang/go/wiki/Modules
After that please try running the test from the shell. First cd into the directory where the test file is present. Then, use go test . -test "<TestName>" -v to run your test. If the issue goes away, you can run the test from IDE and it should work fine.

Go build error: no non-test Go files in <dir>

Getting an error when trying to run go build ./... from my $GOPATH/src .
no non-test Go files in <dir>
The error is correct there are no test files in <dir> but why is that causing a compile error? Is it a bug?
I don't think this is a bug, unless you see somewhere in the docs that contradicts this behaviour you should probably close the issue you've created.
Tests in go normally live in the package they are testing. You have made a new package with package main at the top (invalid if you also have main elsewhere), and then have included no go source files in that tests/main package (invalid as package has no go source files apart from tests, which the compiler complains about explicitly).
Possible solutions for you (assuming this isn't just a hypothetical question):
Move tests for main to test_main.go (this is what readers will
expect)
Add doc.go file to your tests pkg and call it package tests in
both files
The reason for putting tests in the same package is to ensure they have access to the entire package, if you want to split them to another package you'll find you have to test as an external user of the pkg - this may be painful. Main is also a special case as well as you don't normally import it.
Calling it a bug… the build shouldn't fail if the tests compile. Filed here: https://github.com/golang/go/issues/22409
The bug I filed was a duplicate of https://github.com/golang/go/issues/8279 looks like it was broken in 1.3.
First, check your $GOPATH has been set correctly. Learn more at here.
Then, check if any '_' in your file name. Remove these '_'s and try again.
;-)

How to setup Go in GoClipse

I installed goclipse in my eclipse, and setup the preferences as follows :
Preferences->Go->Tools
ProjectExplorer
Now when I create a new GoFile (HelloWorld.src), the file is saved in D:/GO/TestProject/src. But when I build the same file, the bin and pkg folders are empty and hence when I run the file the following error comes :
resource does not have a corresponding go package
Unable to run the code because of this error.
Your project path should be D:\GO\src\TestProject in order to match the workspace expected as described in https://golang.org/doc/code.html
Then, your GOPATH should point to D:\GO (NOT ...\src)
The go tool will automatically use $GOPATH/src, $GOPATH/bin or $GOPATH/pkg when appropiate for each case.
And as icza pointed out, your program should have a package main statement on the top of your go file to be recognized as an executable, unless you want to create a package, in that case, you should name your package as you want.

Resources