Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go linter in VS code not working for packages across multiple files?

Tags:

I have installed the Go extension (version 0.11.4) in Visual Studio Code on MacOS:

enter image description here

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:

enter image description here

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?

like image 760
Kurt Peek Avatar asked Sep 19 '19 20:09

Kurt Peek


People also ask

Is VS code good for go?

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.

How do I enable VSCode modules?

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.


1 Answers

[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.

like image 51
Sathish Ramani Avatar answered Oct 17 '22 07:10

Sathish Ramani