设置GOPATH没有任何效果。

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

setting GOPATH doesn't have any effect

问题

在MacOS Yosemite上,我在我的.profile文件中设置了以下内容:

GOPATH="$HOME/go"
PATH="$PATH:$GOPATH/bin"

但是go env输出的结果是:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

我还在我的主文件夹中创建了.bashrc文件,并将GOPATH变量添加到该文件中,但结果仍然相同。而且似乎在设置这个路径变量之前,我无法安装任何Go包。有什么想法可能出了什么问题吗?

英文:

On MacOS Yosemite, inside my .profile file I have set:

GOPATH="$HOME/go"
PATH="$PATH:$GOPATH/bin"

But go env outputs:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

I also created .bashrc inside my home folder, added the GOPATH variable to the file, but the end result is the same. And it seems that until I set this path variable, I'm unable to install any Go package. Any ideas what could be wrong?

答案1

得分: 1

你需要在声明中加上export,原因是当你从shell启动应用程序时,你的应用程序没有接收到更新后的$PATH。当你export一个变量时,它会将其添加到发送给所有未来应用程序调用的环境变量列表中。

在shell中查看这里以获取有关环境的更多信息。

此外,尝试研究.profile.bash_profile之间的选择,因为如果你在.bash_profile中有类似的导出,并且你没有追加到$PATH中,它将覆盖你的导出。请参阅这个问题获取更多信息。

.profile是shell和bash兼容的,而.bash_profile只是bash兼容的(如果你不知道区别,请使用.bash_profile)。

英文:

You need to have export on your declaration, the reason being that when you start an application from shell your app isn't receiving your updated $PATH. When you export a variable it adds it to the list of environmentals sent to all future application invocations.

See here for more information on the environment within a shell.

Also, try investigating the choices between .profile and .bash_profile since if you have a similar export in your .bash_profile and you don't append to the $PATH it will overwrite your export. See this question for more info.

.profile is shell and bash compatible where as .bash_profile is only bash compatible (if you don't know the difference, use .bash_profile).

huangapple
  • 本文由 发表于 2014年12月21日 22:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/27590125.html
匿名

发表评论

匿名网友

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

确定