简体   繁体   中英

VS Code Go: $GOPATH/go.mod exists but should not

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/go@1.12/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 ?

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.

  go env -w GOPATH=  

GOPATH and Modules

this link

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM