Goland # Ubunu(WSl2) # $GOPATH/go.mod exists but should not

编程入门 行业动态 更新时间:2024-10-23 07:19:55

Goland # <a href=https://www.elefans.com/category/jswz/34/632203.html style=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

本文发布于:2023-11-15 03:06:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Ubunu   Goland   exists   mod   GOPATH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!