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

本文介绍如何在VSCode中使用Go语言避免模块加载问题,包括设置实验性的workspace模块、gowork工具的使用方法,以及针对不同目录结构的优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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] 

来遍历目录下所有文件夹

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值