Michael Whatcott
Michael Whatcott

Reputation: 5965

Resolve GOPATH value (Go 1.8+)

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?

Upvotes: 0

Views: 240

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109367

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.

Upvotes: 2

Related Questions