-1

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?

Dave Mackintosh
  • 2,738
  • 2
  • 31
  • 40
lucassp
  • 4,133
  • 3
  • 27
  • 36
  • Have you tried `export GOPATH="$HOME/go"` in your .profile? – Dave Mackintosh Dec 21 '14 at 14:16
  • @DaveMackintosh it works. But how can I add it to .profile so I don't have to run it every time I need it? – lucassp Dec 21 '14 at 14:18
  • Does anything in that file work? If not, rename it to `.bash_profile` – Dave Mackintosh Dec 21 '14 at 14:20
  • I have the DOCKER config in that file, and other PATH variables, an it does indeed work. But I'll try to rename it and see if Go works. – lucassp Dec 21 '14 at 14:21
  • Don't bother renaming it, just change them to `export GOPATH="$HOME/go"` and `export PATH="$PATH:$GOPATH/bin"` – Dave Mackintosh Dec 21 '14 at 14:22
  • 1
    Although, review this question http://stackoverflow.com/questions/6751252/difference-between-profile-and-bash-profile-on-snow-leopard for the differences between `.profile` and `.bash_profile` and you can make a decision between which one to use. I personally use `.bash_profile` – Dave Mackintosh Dec 21 '14 at 14:23
  • @DaveMackintosh `export` fixes it. What's the difference? – lucassp Dec 21 '14 at 14:23
  • 1
    @lucassp without export the variable remains in the scope of the current script. you should have export before any global env var declaration in your profile file. – Not_a_Golfer Dec 21 '14 at 14:28
  • When you just set a variable it tells the shell to load everything to the right of the `=` into the variable to the left of the `=` whereas `export` is a `'builtin'`. The difference being when shell invokes an application it is passed all of the environmentals, this is everything that is `'exported'` including your `$PATH` :) – Dave Mackintosh Dec 21 '14 at 14:28

1 Answers1

1

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

Community
  • 1
  • 1
Dave Mackintosh
  • 2,738
  • 2
  • 31
  • 40