解析GOPATH值(Go 1.8+)

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

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在运行时实际上并不存在,它只被构建工具使用。GOARCHGOOSGOROOT是在编译时记录的唯一变量,但它们仍然不会影响运行时的执行。

你可以在环境中检查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.

huangapple
  • 本文由 发表于 2017年1月20日 06:00:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/41752503.html
匿名

发表评论

匿名网友

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

确定