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.
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.
I have installed the Go extension (version 0.11.4) in Visual Studio Code on MacOS:
However, I find that the linter does not 'pick up' functions defined in the same package, but in different files. For example, if I create in the same directory a file foo.go with
package foobar
import "fmt"
func main() {
fmt.Println(SayHello())
}
and a file bar.go with
package foobar
func SayHello() string {
return "Hello, world!"
}
then in foo.go I get a linter error that SayHello is an undeclared name:
I've read about a similar issue here (https://github.com/golang/lint/issues/57), but since that issue is five years old I figured it might be fixed by now? Or does golint simply not work across multiple files?
[The original answer is outdated; here is up-to-date information provided by the vscode-go maintainers. The updated answer is now marked as "Recommended" in the Go collective]
The plugin has changed a lot since 2019.
In 2021, Go Modules became the default which may have changed how the program is built and analyzed.
The vscode-go plugin uses gopls as the language server by default. Note that in 2019, there were two different language servers and gopls was still in experimental mode.
golint was deprecated.
If you still have a similar issue, it's likely that you are seeing a different problem.
Please check the followings:
Do you have go.mod? Otherwise, initialize your working module and restart the language server or reload the window.
Is the go.mod file in the root directory of your workspace? See gopls workspace setup
guide for complex setup.
Do you use build tags or other build constraints? Then see issue 29202. You may need to configure "go.buildTags" or "go.buildFlags".
If you expect lint errors from linters like staticcheck, golangci-lint, ..., check "go.lintOnSave" is set to the right scope.
If you notice that restarting the language server ("Go: Restart Language Server" command) fixes your issue, that's a gopls bug. Please consider to file an issue in github.com/golang/vscode-go following the troubleshooting guide.
Otherwise, please open a new question with details.
----- Original answer -------
I faced same problem. I found that I got into this problem after enabling "Go language server" which is an experimental feature. I disabled it in VS code settings->Go Configuration and after that the problem went away.
Update VS Code Go Tool might help.
Command + Shift + P -> Go: Install/update tools
Install all tools and restart VS Code.
May 2022 update:
This solution only works if you haven't installed the helper tools. Normally after you installed these packages it'll work right away with the default configuration, if you still have a problem, take a look at the answer above.
The cause of this warning for me was the setting go.lintOnSave, which was set to file. Changing the value to package made the linter correctly pick up the types defined in other files.
For people who ended up here:
The plugin has changed a lot since 2019.
In 2021, Go Module became the default which may have changed how the program is built and analyzed.
The vscode-go plugin uses gopls as the language server by default. Note that in 2019, there were two different language servers and gopls was still in experimental mode.
golint was deprecated.
If you still have a similar issue, it's likely that you are seeing a different problem.
Please check the followings:
Do you have go.mod? Otherwise, initialize your working module and restart the language server or reload the window.
Is the go.mod file in the root directory of your workspace? See gopls workspace setup
guide for complex setup.
Do you use build tags or other build constraints? Then see issue 29202. You may need to configure "go.buildTags" or "go.buildFlags".
If you expect lint errors from linters like staticcheck, golangci-lint, ..., check "go.lintOnSave" is set to the right scope.
If you notice that restarting the language server ("Go: Restart Language Server" command) fixes your issue, that's a gopls bug. Please consider to file an issue in github.com/golang/vscode-go following the troubleshooting guide.
Otherwise, please open a new question with details.
Update Install/update tools for GO
Open your code as main project in VS Code and avoid multiple projects/workspace in the same VS Code.
**
Single Project VS Code
**
**
Avoid Multiple Project in VS Code
**
If you run across this and are NOT using modules, then adding "go.useLanguageServer": false will disable gopls and return you to your former environment. (meaning vscode will now recognize functions and structures defined in multiple files in the same package)
In my case it was a missing go.mod file. I fixed with the following command:
go mod init example.com/myProject/myModule
Of course you should use a more reasonable module name.
Make clean uninstall of vscode and then it's work fine again...
add sudo if you needed to
rm -rf $HOME/Library/Application\ Support/Code
rm -rf $HOME/.vscode
Remove vscode from application
Download vscode and install again
One possible reason:
If you are referencing a function/variableis declared in a test file (*_test.go) from a non test file, this error would be thrown.
In my case, I just restarted VS Code and the error went away.
After almost pulling my hair out, I found that linting was working but I had many files with errors. I haven't yet found the hierarchy followed but fixing problems in one file subsequently led to it correctly linting another file. I think it follows the execution tree, although I haven't validated this.
I found this annoying as it can mistakenly lead you to think that linting is not working, while in fact, it's lining a file that you're not currently focussed on, especially if you have generated files that you're not interested in.
Another solution might be that you need to have the folder opened in VS Code with the go.mod file included. So you might have a folder structure that looks like workspace/application/modules/xyz.go. If you have the go.mod file in the application folder and modules is the folder you have open in VS Code it will complain.
I came across this issue by having the go extension installed, and attempting to utilize the same package name with a module under a different directory.
Files at Root: main.go, a.go, and go.mod
Sub-directory: nested/b.go
Problem: Attempting to label b.go as package main when it is under a different directory.
Solutions:
Move b.go up to project root and retain package name; all works as expected,
or
Change package name of b.go from package main to package nested, then add imports for b.go to main.go via:
// main.go
package main
import "example/nested"
func main() {
A()
nested.B()
}
and b.go:
// b.go
package nested
import "fmt"
func B() {
fmt.Println("Hello from B")
}
I am testing this very simple Go code on MacOS using VS Code. The project consists of these sample packages / files:
azure.com/myproj/cmd/service/main/main.go
azure.com/myproj/cmd/service/service.go
azure.com/myproj/cmd/service/tests/test.go
azure.com/myproj/internal/common/common.go
On the terminal command line, everything builds and all tests pass:
go build . // (works in every folder)
go test . // (tests work and pass)
However, from VS code I have 2 problems:
1. Imports from package to package do not work at all. For example:
package test
import (
service "azure.com/myproj/cmd/service" // VS complains on this line when running the test.
)
The command that VS code runs is not just "go test . ". It passes extra parameters that include what seems to be a cached path, which I tried deleting from the file system, but it did not have an effect. This is what VS.code's Output tab contains:
Go Tests tab:
unknown import path "azure.com/myproj/cmd/service":
cannot find module providing package
azure.com/myproj/cmd/service
Go tab:
/Users/computername/go/src/azure.com/projname/cmd/service/tests
>Finished running tool: /usr/local/bin/go test -c -o
/var/folders/q5/hm9v_6x53lj0gj02yxqtkmd40000gn/T/vscode-goKGOMES/go- code-check azure.com/myproj/cmd/service/tests
can't load package: package
azure.com/myproj/cmd/service: unknown import path
"azure.com/myproj/cmd/service": cannot find module
providing package azure.com/myproj/cmd/service
I do not understand what VS code is doing above with hm9v_6x53lj0gj02yxqtkmd40000gn and how I can change it. It looks like a cache.
So to summarize: When testing through VS Code, i do not understand why it's using the command that it is to run the tests (above), and why it can not find the imports, which the regular "go build . " and "go test . " commands have no problem with through the terminal.
Once again: From the terminal command line everything builds and all tests pass.
Seems to clearly be a VS Code related issue.
I found the answer to my problem. VS did not like the file path structure for the project. Here are the changes I made:
Moved main.go from azure.com/myproj/cmd/service/main/main.go up a level to azure.com/myproj/cmd/service/main.go
Moved the service implementation (service.go) into the internal folder: azure.com/myproj/internal/service/service.go
Moved the tests into the internal folder:
azure.com/myproj/internal/service/test/service_test.go
How VS code is able to run the tests without complaining of the imports not being found.
So now I only have the main.go in my /cmd/service folder. Everything lives in Internal, including tests. I suspect that burying main.go into a subfolder is what was confusing VS Code.
I am creating a project in go. Since there is already a lot of things at the root of the repository of the project (doc, README.md...), I want all the source code to go in a folder src, and all the test code in a folder named test :
\go
\src
\github.com
\user
\my_project
\src
main.go
some_func.go
\test
test_main.go
test_some_func.go
\doc
README.md
But I have two issues :
The build command is not working while I am in the my_project folder. I have to go in the my_project/src to successfully run build. I want to do it from the my_project folder. How to inforce go to understand that the source for my_project is in the src code ?
Then the executable produced by the go install command is named src. How to change the name of that executable ?
I want all the source code to go in a folder src, and all the test code in a folder named test :
Go has a way that it organizes source code. Do not fight this. It is how the system works. Organize your code the way Go wants you to. Do not try to force Go to work the way you have learned working in some other language. Every language has its own ways of doing things. None of them are "correct." Like Java, Go has very specific ideas of what you're supposed to do. Do it that way. (This isn't an argument about whether Go is "right" or Go is "wrong." Go is Go, and it does things in the Go way.)
In particular, you should not create another "src" directory. There is already a "src" directory at the top of the "go" tree. If you create another, redundant, "src" directory, then the package name for your project is "github.com/user/my_project/src" which is likely not what you want.
To make the executable be named what you want, put it in a directory named what you want (probably "my_project"). Put test files with the files they test. This is how go works.
So your tree should look like:
\go
\src
\github.com
\user
\my_project
main.go
some_func.go
main_test.go
some_func_test.go
\doc
README.md
Attempts to do something other than this is going to blow up over and over again, and questions of "how do I make the build system do this other thing" will continually return "put your code in the way the build system expects."
For details on what Go expects, and how you should organize your code, see GOPATH environment variable in the Command Go documentation. Once you've built your system this way for a while, you will start to see where you can deviate from it (like creating other directories for test utilities, but not test cases). Don't start deviating until you've tried it the standard Go way.