Reputation: 57421
I'm trying to use the Go features of VS Code (like 'Go to definition') in a package with a go.mod
. However, if I go into the repository's main directory (with the go.mod) and code .
, I get this error:
Error loading workspace folders (expected 1, got 0) failed to load view for file:///Users/kurt/Documents/http-enrollment-portal: err: exit status 1: stderr: $GOPATH/go.mod exists but should not
I've read that I might have to update the gopls
language server, so I pressed Cmd + Shift + P and selected "Go: Install/Update Tools" and selected gopls
; however, this gives me the same error:
go.toolsGopath setting is not set. Using GOPATH /Users/kurt/Documents/http-enrollment-portal:/Users/kurt/go
Installing 1 tool at /Users/kurt/Documents/http-enrollment-portal/bin in module mode.
gopls
Installing golang.org/x/tools/gopls FAILED
1 tools failed to install.
gopls:
Error: Command failed: /usr/local/opt/[email protected]/bin/go get -v golang.org/x/tools/gopls
$GOPATH/go.mod exists but should not
$GOPATH/go.mod exists but should not
The strange thing is that 'Go to definition' does work in other repositories. I suppose a possible solution is to set the go.toolsGopath
?
Upvotes: 4
Views: 12908
Reputation: 11
you have to go to the folder where you created your go project and delete the go.mod file after you create another file with the command : go mod init nameproject on your command prompt (power
Upvotes: 1
Reputation: 21
go env -w GOPATH=
When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin).
Upvotes: 2
Reputation: 11
I had a similar issue. So to explain I am using https://www.mongodb.com/blog/post/quick-start-golang-mongodb-starting-and-setup
To try Mongodb with Go. In VS Code I first installed go mod init quickstart
Then I used go get go.mongodb.org/mongo-driver
and got the error $GOPATH/go.mod exists but should not
.
I had previously set my GOPATH according to https://www.freecodecamp.org/news/setting-up-go-programming-language-on-windows-f02c8c14e2f/ (see Phase 3 on this page).
I went into my Windows Environment Variables and deleted my GOPATH. On the Windows 10 start menu go to System
then Advanced system settings
under the Advanced
tab click the Environment Variables
button and I deleted my GOPATH
variable.
Next back in VS Code I again try go get go.mongodb.org/mongo-driver
and it works.
Upvotes: 1