Go设置一个工作区打开多个项目

在vscode使用go的时候,如果没有在gopath下运行,可能会出现

Error message "go: go.mod file not found in current directory or any parent directory; see 'go help modules'"

或者

"Error loading workspace: You are outside of a module and outside of $GOPATH/src. If you are using modules, please 
open your editor to a directory in your module. If you believe this warning is incorrect, please file an issue:
https://github.com/golang/go/issues/new."

虽然不会有什么影响,但是看起来很烦。

在早期的版本中(go 1,17及以前),可以通过在setting.json中添加

"gopls": {
    "experimentalWorkspaceModule": true,
}

如果是1.18以后的版本,可以使用go work

在工作区的根目录下运行

go work init
go work use [floder-name] 


比如我的目录是这样的结构。就可以运行

go work use gin1  // Or go work use .\gin1\

如果有在目录下有多个文件夹,可以使用

go work use -r [floder-name] 

来遍历目录下所有文件夹

猜你喜欢

转载自blog.csdn.net/majiayu000/article/details/128047281