英文:
When should you set the $GOPATH variable?
问题
有人可以解释一下何时设置$GOPATH吗?我知道它的默认位置是C:\Users\Username\go,但如果我所有的编码项目都在Z:<codelocation>中,该怎么办?
英文:
Can anyone explain when to set $GOPATH? I know it's in the default, C:\Users\Username\go, location, but what if all my coding projects live in Z:<codelocation> ?
答案1
得分: 9
根据文档:
Go路径用于解析导入语句。
GOPATH环境变量列出了查找Go代码的位置。
在使用模块时,GOPATH不再用于解析导入。然而,它仍然用于存储已下载的源代码(在GOPATH/pkg/mod中)和已编译的命令(在GOPATH/bin中)。
假设您正在使用模块,您应该使用模块,那么无论您将项目放在何处,设置GOPATH都不会影响您的项目。它只需要在某个地方设置,以便Go工具有一个缓存来存储库和二进制文件。
英文:
From the documentation:
> The Go path is used to resolve import statements.
>
> The GOPATH environment variable lists places to look for Go code.
>
> When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin).
Assuming you're using modules, which you probably should be, the setting of GOPATH will not impact your projects regardless of where you put them. It only needs to be set somewhere so the Go tools have a cache to store libraries and binaries.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论