在Go语言中存在环境变量的问题。

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

Problems with environment variables in go

问题

我试图在我的.go文件上运行go install,但似乎失败了。失败的原因是我的GOBIN环境变量没有设置。然而,当我使用echo命令时,我确实得到了它已经设置的结果,因为我的.bashrc和.bash_profile文件确保它被设置。然而,在go env中它没有被设置。由于某种原因,go在实际设置了环境变量时却无法识别它。

然而,如果我在我的shell上手动设置为:

me$ export GOBIN=$GOBIN

现在go env决定识别它,尽管我在我的.bashrc文件中有明确的导出行,并且我的echo确认它已经设置。有人知道为什么go的行为如此奇怪吗?

我尝试过的事情/参考

我的操作系统

mac osx mavericks。

GO版本

-我的go版本是go version go1.2 darwin/386。当我运行

go version

时,我得到:

go version go1.2 darwin/386

go env识别的环境变量

运行

go env

在我的终端中显示:

GOARCH="386"
GOBIN=""
GOCHAR="8"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_386"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread -fno-common"
CXX="g++"
CGO_ENABLED="1"

我的.bashrc和.bash_profile的样子

我在我的.bash_profile中引用了我的.bashrc文件。即在我的.bash_profile中有以下代码:

if [ -f ~/.bashrc ]; then
source ~/.bashrc #executes for bash subshells
fi

我还尝试手动(手动指的是作为人类在bash中显式输入)引用我的.bash_profile(因为这样会运行我的.bashrc文件),但go env仍然无法识别它。

只有当我在shell中直接输入以下命令时:

me$ export GOBIN=$GOBIN

go env才会返回我想要的结果:

GOARCH="386"
GOBIN="/Users/brando90/go/bin"
GOCHAR="8"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_386"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread -fno-common"
CXX="g++"
CGO_ENABLED="1"

英文:

I was trying to run go install on my .go files however, it seems to fail. It fails because my GOBIN environment variable is not set. However, when I echo it I do get that its set because my .bashrc and .bash_profile files make sure its set. However, it is not set in go env. For some reason go doesn't recognize its set when it actually is set.

However, if I manually set on my shell as:

me$ export GOBIN=$GOBIN

now go env decides to recognize it, eventhough I have the explicit line on my .bashrc file exporting it and my echo confirms that its set. Does someone know why go is acting strange?

Things that I have tried/Reference

My Operating system

mac osx mavericks.

GO VERSION

-my go version is go version go1.2 darwin/386. When I run

go version

I get:

go version go1.2 darwin/386

What go env recognizes and environment variables

Running

go env

displays in my terminal:

GOARCH="386"
GOBIN=""
GOCHAR="8"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_386"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread -fno-common"
CXX="g++"
CGO_ENABLED="1"

How my .bashrc and .bash_profile look like

I am sourcing my .bashrc file in my .bash_profile. I.e. here this piece of code in my .bash_profile:

if [ -f ~/.bashrc ]; then
  source ~/.bashrc #executes for bash subshells
fi

I have also tried to manually (by manually I mean typing it on the bash explicitly as a human) source my .bash_profile (since that will run my .bashrc file anyway) and go env still fails to recognize it.

Only when I literally type in my shell

me$ export GOBIN=$GOBIN

does go env return what I want:

GOARCH="386"
GOBIN="/Users/brando90/go/bin"
GOCHAR="8"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_386"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread -fno-common"
CXX="g++"
CGO_ENABLED="1"

答案1

得分: 3

在你的相关shell配置文件中尝试以下内容:

export GOPATH=/path/to/your/go/workspace
export PATH=$GOPATH/bin:$PATH

不要设置GOBIN,因为在99%的情况下它是没有用的(即单用户机器;参见cmd文档)。确保在进行这些更改后取消设置GOBIN

Go会知道在GOPATH/{bin, pkg, src}中安装二进制文件,这是Go为你处理的。另一方面,你的shell需要知道将$GOPATH/bin添加到你的路径中,这样你就可以直接运行Go二进制文件。

英文:

Try this in your relevant shell profile:

export GOPATH=/path/to/your/go/workspace
export PATH=$GOPATH/bin:$PATH

Don't set GOBIN as it's not useful for 99% of cases (i.e. single user machines; see the cmd docs). Make sure to unset GOBIN after making these changes.

Go will know where to install binaries as GOPATH/{bin, pkg, src} is something Go handles for you. Your shell, on the other hand, needs to know to add $GOPATH/bin to your path so you can run Go binaries directly.

答案2

得分: 3

我想确保以后其他人来查看我的问题时,他们知道问题所在以及我是如何解决的。

我的一个问题是我的bash文件中有一个导出的方式如下:

export $GOBIN

而不是

export GOBIN

第二种方式是错误的,当我执行以下操作时进行了修正:

export GOBIN="$GOPATH/bin"

所以之前无法工作的原因是因为我的特定错误。然而,我还学到了其他一些一般性的东西。我了解到当我们执行"go install"时,实际上不需要设置GOBIN为我的Unix环境变量。当GOPATH变量设置正确时,当我们在某个东西上执行"go install"时,Go会知道bin目录存在于GOPATH/bin中。所以正确设置的重要事项是GOPATH而不是GOBIN。

正如elithrar之前提到的,如果你希望你的shell能够运行你在go工作区中编译的go文件生成的命令,你需要将GOPATH/bin添加到你的PATH环境变量中。

感谢大家的帮助!

英文:

I want to make sure for future references when other people come to see my question, they know what were the problems and how I fixed it.

One of the problems my bash files had were that one of them exported in the following way:

export $GOBIN

instead of

export GOBIN

which the second was incorrect, which was fixed when I did:

export GOBIN="$GOPATH/bin"

So the reason that did not work was because of my particular mistake. However, there were some other things that I learned in general. I learned that when one does "go install", we do not actually need GOBIN to be set for my Unix environment variables. When the GOPATH variable is set correctly, when one does go install on something, Go knows that the bin directory exists as GOPATH/bin. So the important thing to set correctly is the GOPATH rather than the GOBIN.

As elithrar mentioned earlier, you need to put in your PATH environment variable the GOPATH/bin if you want your shell to be able to run commands from compiled files from go that you might have made in your go workspace.

Thanks for everyone for the help!

答案3

得分: 2

尝试将export行放在你的.bash_profile中,而不是.bashrc中。

我读到终端始终将bash作为登录会话运行,因此会加载.bash_profile,但不会加载.bashrc

请参考https://stackoverflow.com/questions/7501678/set-environment-variables-on-mac-os-x-lion获取更多信息。

英文:

Try putting the export line in your .bash_profile instead of .bashrc.

I've read that Terminal always runs bash as a login session, and therefore will source .bash_profile, but not .bashrc.

See https://stackoverflow.com/questions/7501678/set-environment-variables-on-mac-os-x-lion for a bit more info.

huangapple
  • 本文由 发表于 2014年1月27日 09:53:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/21371805.html
匿名

发表评论

匿名网友

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

确定