User12547645
User12547645

Reputation: 8447

vscode does not detect errors golang

My vscode does not detect errors in golang.

Example:

package somepackage

import "fmt"

func f(name string) string {
    name = 1
    return name
}

This should throw a type error, but it does not. I never get any errors.

My settings.json contains

"go.gopath": "some/path", 
"go.vetOnSave": "package",
"go.lintOnSave": "package",
"go.testOnSave": true,
"go.buildOnSave": "package",
"go.coverOnSave": true 

I was able to run go: install/update Tools. All tools are installed successfully.

I am also able to run debug a .go file in vscode.

Upvotes: 2

Views: 8382

Answers (1)

User12547645
User12547645

Reputation: 8447

As @pwaterz pointed out, the solution to my problem was to add "go.goroot: /some/other/path".

The reason that vscode was not able to detect errors was, that there are different go versions on my computer. Adding the goroot and running go: install/update Tools solved the problem.

---- Edit: Multiple go versions ----

Had multiple conflicting go versions on my Mac, pulled in via brew. Fixed the problem with a reinstall.

  • Uninstall go and also run brew uninstall go
  • Reinstall go
  • Set environment variables in your .bash_profile or similar. Compare here.
  • Apply the changes to your profile by running e.g. source .bash_profile
  • Restart VSCode
  • In settings.json set "go.goroot": "/usr/local/go"
  • Run go: Toggle workspace trust space to make sure changes to settings.json are applied (you have to trust your workspace for that)
  • go: Install/update tools and select all

---- Edit: Incorrect root folder ----

Make sure that you open the root folder of your project and not a sub-folder of your project. That may cause in invalid import paths otherwise

---- Edit: Broken language server ----

Upvotes: 6

Related Questions