vscode does not detect errors golang - go

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.

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 ----
Try to run go: restart language server
You may see Error loading workspace
run go mod tidy and try again

Related

autocomplete/intelliSense not working for golang in vscode

I tried below things
Restart of VSCode
Uninstalled and installed Go plugin from google team for VSCode
Ran gopls server in code repo using go get golang.org/x/tools/gopls#latest
Finally, I was able to solve this problem. My project was required export CGO_LDFLAGS_ALLOW=".*" env var to be set.
So, I added below section to the settings.json of the Go plugin for vscode
"go.toolsEnvVars": {
"CGO_LDFLAGS_ALLOW":".*"
}
After that, all the modules were loaded successfully and VSCode was able to provide intelliSense.
Sometimes vscode do not detect the GOPATH and GOROOT, try to set them manualy in the user settings (JSON) like so :
"go.gopath": "..path..",
"go.goroot": "..path..",

Unable to automatically install go extensions in vscode

I am getting started with Go and would like to use Visual Studio Code for editing. I successfully installed go on my Ubuntu Computer.
My GOPATH (go env) is set to the same value as go.gopath in settings.json. If I run Go: Current GOPATH it outputs the correct path.
However, I am not able to install the go extensions. If I click on Install all I get output similar to this:
Installing 1 tool at home/jan/go/bin
dlv
Installing github.com/go-delve/delve/cmd/dlv FAILED
1 tools failed to install.
dlv:
Error: Command failed: /snap/bin/go get -u -v github.com/go-delve/delve/cmd/dlv
I seem to be able to just install packages manually with /snap/bin/go get -u -v github.com/go-delve/delve/cmd/dlv. The command runs without errors, but VSCode seems to be unable to find dlv.
My settings.json:
{
"terminal.integrated.rendererType": "dom",
"go.useLanguageServer": true,
"go.formatTool": "gofmt",
"go.lintOnSave": "file",
"go.vetOnSave": "package",
"go.buildOnSave": "package",
"go.lintTool": "golint",
"go.gopath": "home/jan/go"
}
I have git installed on my computer. It works without a problem.
Your GOPATH is not set correctly in settings.json.
"go.gopath": "home/jan/go"
This path is relative, and so it would be appended to whatever your working directory is, with the obvious result of not being able to find the path as it won't exist.
You can either set it correctly, or delete it and the GOPATH environment variable will be used. (I run it this way.)
"go.gopath": "/home/jan/go"

GoLand IDE not correctly compiling. How to setup correct paths?

I am trying to setup GoLand correctly to be able to use it with Go.
I'm trying to run the follow simple HelloWorld go project.
package HelloWorldProject
import "fmt"
func main(){
fmt.Printf("Hello")
fmt.Printf("1+1 = ", 1+1)
}
This is my console's results:
GOROOT=/usr/local/Cellar/go/1.10/libexec #gosetup
GOPATH=/Users/jeanmac/go #gosetup
/usr/local/Cellar/go/1.10/libexec/bin/go build -i -o /private/var/folders/r5/rfwd1cqd4kv8cmh5gh_qxpvm0000gn/T/___Hello /Users/jeanmac/go/src/github.com/jpere547/HelloWorldProject/Hello.go #gosetup
Compilation finished with exit code 0
I am on Mac OS and I installed Go using Brew.
Results of brew info go:
go: stable 1.10 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.10 (8,150 files, 336.9MB) *
Poured from bottle on 2018-03-22 at 19:38:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Required: macOS >= 10.8 ✔
==> Options
--without-cgo
Build without cgo (also disables race detector)
--without-race
Build without race detector
--HEAD
Install HEAD version
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
The GoLand configurations is below:
GOROOT
GOPATH
It looks like you are trying to run a non main package. Specifically, instead of package HelloWorldProject, you should use package main. After that the IDE will be able to not only build but also run the package.
When you do go build something.go or go isntall something.go that only builds/installs the go package to give you an executable. You will need to run that executable.
The easiest way to run simple Golang programs is simply use go run something.go that will run your go file.
As long as your GOPATH is correctly set it should work
you can try to change the package name instead of main
There is an option in configuration to run after build. I think you are missing to set it's configuration.Steps:
1. Go to Run
2. Select "Edit configuration"
3. Select the check-box below output directory field specifying "Run after build".

How to fix delve "can't load package: package internal" error

So I installed VSCODE on my windows machine as my main golang IDE and I guess my overall lack of go knowledge is making me experience this seemingly unsolvable error:
I installed delve and wanted to use it as my debugger in vscode-go. I did everything as mentioned in the readme on https://github.com/Microsoft/vscode-go , yet for some reason when I run dlv debug I get error "can't load package: package internal: no buildable Go source files in c:\go\src\internal exit status 1". I couldn't find anything related to this folder on the internet, but I have never myself entered the path to src/internal in any of the config files. So this is making me really confused, since I thought if it was a common folder someone should have atleast gotten the same error once.
It occurs both in the vscode and in command prompt, also if dlv test is run. I also know it is not the issue with my folder structure/env variables since it complains about my package files missing too if I remove them in addition to also giving the same c:\go\src\internal error. It seems as if there is a setting somewhere to check that folder for source files too, but I can't find the setting and the dlv debug crashes after giving me the error.
Got same issue with latest delve and vscode:
can't load package: package internal: no buildable Go source files in
C:\Coding\Go\src\internal
and 'go build' working without any errors
Have to fallback to previous version of delve to get it working. It seems delve broke something or just need to update some things. Seems it enough just to replace dlv.exe in %GOPATH%\bin folder. I'm not sharing my version because it exe, but you can find it or build from sources
I solved my problem that looks like same problem by changing launch.json file.
// before
"program": "${workspaceRoot}
// after changing
"program": "${workspaceRoot}/src/github.com/myproje/hello",
I had the same problem. When i check, {fileDirName} points my .vscode folder. So i've just given my path manuelly and it works.
I've replaced this
"program": "${fileDirname}"
with this
"program": "c:/Go/src/MyGoProject"
same problem (vscode/linux/go1.8)
The Solution:
change in launch.json >
"program": "${fileDirname}"
If Go is installed correctly, this error message is saying you are trying to import an external package called "internal"
Compile an empty program with no imports to see if the issue persists eg.
package main
func main() {
}
You should not see any errors here. If you do then that is most likely an indication Go is not installed properly.

Golang another unrecognized import path

When i am trying to install golint (or gin, for example) i get "unrecognized import path error".
I know that there are many same questions, but main answer is to check environment variables.
There is screenshot of my environment variables, my folders and console with error.
I tried to install go both with .msi installer and just by copying files and setting env var manually. I got the same results.
There is a go get issue currently discussed
my go tools were out of date, but go get could not update them because they switched form mercurial to git at some point. Deleting the whole golang.org/x/tools directory in my GOPATH and reinstalling fixed the issue.
Make sure you have the latest go, and try with a fresh empty GOPATH folder, to see if the issue persists.

Resources