你应该在什么时候设置$GOPATH变量?

huangapple go评论92阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年5月26日 05:28:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/72384370.html
  • environment-variables
  • go
  • gopath
  • gopls
  • visual-studio-code
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定