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

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?



Solution 1:[1]

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

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.

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

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:

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.

Solution 5:[5]

  1. Update Install/update tools for GO
  2. Open your code as main project in VS Code and avoid multiple projects/workspace in the same VS Code. **

Single Project VS Code

** Single Project VS Code

**

Avoid Multiple Project in VS Code

**

Avoid Multiple Project in VS Code

Solution 6:[6]

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)

Solution 7:[7]

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.

Solution 8:[8]

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.

Solution 9:[9]

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.

Solution 10:[10]

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

Solution 11:[11]

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:

  1. Move b.go up to project root and retain package name; all works as expected,
  • or
  1. 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")
}

Screen Shot

Solution 12:[12]

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.