Problems with environment variables in go
Solution 1
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.
Solution 2
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!
Solution 3
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 Set environment variables on Mac OS X Lion for a bit more info.
Comments
-
Charlie Parker almost 2 years
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"
-
Charlie Parker over 10 yearsI have my .bash_profile sourcing my .bashrc file. I should have said that in my question.
-
Charlie Parker over 10 yearsThis answer my still be helpful for someone else, so I will still like it. Thanks :)
-
Intermernet over 10 yearsStrange. What happens if you do a
source .bash_profile
instead of running theexport
manually? -
Charlie Parker over 10 yearsI know its so odd! Only when I type it does go env recognize it. I did try source .bash_profile but go still doesnt recognize it. Only when I actually type export GOBIN=$GOBIN does it decide that now its ok to recognize it...
-
Charlie Parker over 10 yearsThanks elithrar! Just as a side question, when is GOBIN useful then? Is go now only using my GOPATH to do everything it needs?
-
Charlie Parker over 10 yearsIn the end it happened that I was had export $GOBIN instead of export GOBIN
-
elithrar over 10 yearsAs per the Go cmd docs: "If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin." In other words, it's useful if you want to (perhaps) set a universal, system-wide location for installed Go binaries/programs. In most cases, you do not :)
-
kmiyashiro about 7 yearsOn windows, if I do not set GOBIN, it does not install the binaries in anywhere I can find. I have my GOPATH setup correctly, but go does not install in %GOPATH%/bin. Super confusing.