autocomplete/intelliSense not working for golang in vscode - go

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..",

Related

Can't use installed packages in GoLand

I'm using Go 1.16 and GoLand 2020.3.2.
If I type import "github.com/a/b", GoLand will ask me to install the package. I click install and it does so successfully. The library is installed under C:\Users\user\go\pkg\mod. But GoLand looks for the package in C:\Users\user\go\go1.16\src.
This my GOPATH C:\Users\user\go and GOROOT C:\Users\user\go\go1.16.
What am I doing wrong?
It looks like a problem with GoLand's handling of GOPATH based projects.
I created a ticket for this on our tracker.
As a workaround, you can create a go.mod file or a project based on Go Modules instead and the problem will be solved.

About warning popup after gopls installation in vscode

I installed gopls using command set GO111MODULE=on and golang.org/x/tools/gopls#latest to use gopls.
After that, I restarted the program and whenever I write and save the source code, a warning window appears like the picture below.
I'm not sure what this warning means.
I am working on GOPATH and have all the packages I need.
But I don't know why i need a module here.
As mentioned in "GOPATH in the VS Code Go extension"
Out of the box, the extension uses the value of the environment variable GOPATH. From Go 1.8 onwards, if no such environment variable is set, then the default GOPATH as deciphered from the command go env is used.
Check if you have set go.gopath or go.inferGopath.
Check what the returned value of go env GOPATH is.
More generally, it is a good idea to initialize a module at the root of your project (wherever your project is, GOPATH or not)
cd /path/to/my/project
go mod init myproject
Some issues mentioned that same error message
You are neither in a module nor in your GOPATH.
Please see https://github.com/golang/go/wiki/Modules for information on how to set up your Go project.
Issue 36120 for instance said:
I believe this is because my GOPATH is a colon-separated string:
GOPATH="/Users/user/go:/Users/user/go-work"
But... that was fixed in CL 211304 and commit 74e303f in gopls v0.3.2.

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

GoType issues with SublimeLinter

I'm a bit new to Go, and I've been trying to use the SublimeLinter-contrib-gotype package to lint my files. It works well for the most part, but for some reason it throws an error if I try to import a Go package from GitHub. I've been trying to use the simple example from the Echo framework. When I run the code, it works fine but for some reason the linter is causing issues.
It throws an error saying:
could not import github.com/labstack/echo (can't find import: )
I've already run the go get github.com/labstack/echo command, but it doesn't seem to have helped.
I've attached a screenshot as well:
And here is a link to the code I was using.
I'd faced the same problem. Try running
launchctl setenv GOPATH $GOPATH
and restarting ST, this won't work after a reboot.
Source
If you use Go Sublime, the following should work better.
Sublime Text menu > Preferences > Package Settings > GoSublime > Settings - Default / User
"shell": ["/usr/bin/bash"],
"env": {"GOPATH": "/Users/username/gopath/"},

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