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?
In the Go Developer Survey 2020 Results, 41% of respondents chose Visual Studio Code as their most preferred editor for Go. This makes Visual Studio Code the most popular editor for Go developers. Visual Studio Code and the Go extension provide IntelliSense, code navigation, and advanced debugging.
Add the setting "go. formatTool": "goimports" and then use Go: Install/Update Tools to install/update goimports as it has recently added support for modules.
[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.
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:
go.mod
? Otherwise, initialize your working module and restart the language server or reload the window.go.mod
file in the root directory of your workspace? See gopls
workspace setup guide for complex setup."go.buildTags"
or "go.buildFlags"
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With