Ubunu(WSl2) # $GOPATH/go.mod exists but should not"/>
Goland # Ubunu(WSl2) # $GOPATH/go.mod exists but should not
问题剖析
如果你设置了环境变量$GOPATH,Go 将默认在 $GOPATH/pkg/ 中下载和导入依赖库,而 go.mod文件则是将当前工作目录作为项目根目录,从 ./pkg/ 中下载和导入依赖库。因此,当$GOPATH 指向 go.mod 所在目录时,就会产生矛盾。
所以解决思路也是显而易见的:你在执行 go mod init 命令后将 $GOPATH 也指向了项目目录,所以你应该首先选择通过命令行 unset GOPATH。
最佳实践
从Go1.13开始,使用Go Modules管理Go项目,放弃$GOPATH:
创建并进入项目目录
开启Go Modules:export GO111MODULE=on;
项目初始化:go mod init。
更多推荐
Goland # Ubunu(WSl2) # $GOPATH/go.mod exists but should not
发布评论