dreamwind1985 2014-01-27 01:53
浏览 47
已采纳

go中的环境变量问题

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"
  • 写回答

3条回答 默认 最新

  • dongnai5905 2014-01-27 02:57
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行