英文:
Resolve GOPATH value (Go 1.8+)
问题
在Go 1.8+版本中,GOPATH
环境变量是可选的。当未设置时,GOPATH
默认为$HOME/go
。是否有某个标准库包中的变量或函数可以显示GOPATH
的最终值(我希望有类似runtime.GOROOT()
的东西,即使在未设置GOPATH
环境变量的情况下也能推断出来)?
英文:
In Go 1.8+ the GOPATH
environment variable is optional. When not set, GOPATH
defaults to $HOME/go
. Is there a variable or function in some standard library package that will show the final value of GOPATH
(I'm hoping for something like runtime.GOROOT()
even when it is inferred in the case that no GOPATH
environment variable has been set?
答案1
得分: 2
GOPATH
在运行时实际上并不存在,它只被构建工具使用。GOARCH
、GOOS
和GOROOT
是在编译时记录的唯一变量,但它们仍然不会影响运行时的执行。
你可以在环境中检查GOPATH
,如果不存在,可以回退到$HOME/go
。然而,这仍然不能保证你得到的是用于构建二进制文件的GOPATH
,因为应用程序可能不是由同一用户构建的,甚至不是在同一系统上构建的。
英文:
GOPATH
doesn't technically exist in the runtime, it's only used by the build tools. GOARCH
, GOOS
, and GOROOT
are the only variables recorded at compile time, but they still do not effect the execution of the runtime.
You can check for GOPATH
in the environment, and fallback to $HOME/go
if it's not there. However, this still doesn't guarantee that you're getting the GOPATH
used to build the binary, since the application may not have been build by the same user, or even on the same system.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论