一个workspace下多个项目时,vscode环境报错:
gopls was not able to find modules in your workspace. When outside of GOPATH, gopls needs to know which modules you are working on. You can fix this by opening your workspace to a folder inside a Go module, or by using a go.work file to specify multiple modules
解决方法:
cd <project root dir>
go work init
go work use <project 1>
go work use <project 1>
go work use <project 1>
报错消失
修复上述报错后, vscode环境可能还会报错:
packages.Load error: err: exit status 2: stderr: go: no such tool "compile"
此问题大概率是GOROOT环境变量设置不对,如下图点击,修改GOROOT环境变量的值为go安装的位置,我的位置是/usr/local/go
在这个里面也可以修改其它go的环境变量,快捷又方便