Doug Smith
Doug Smith

Reputation: 29326

Why does Golang say my GOPATH isn't set when I set it the command directly before?

This is becoming very frustrating. I set my GOPATH in my ~/.bash_profile, but Go still says it's not set. So I set it manually, and it still says.

computer:~ doug$ export GOPATH=~/Dropbox/golang
computer:~ doug$ sudo go get -u golang.org/x/tools/cmd/...
package golang.org/x/tools/cmd/...: cannot download, $GOPATH not set. For more details see: go help gopath

What am I doing wrong here? If I type goenv it even lists it as being set to the right path.

Upvotes: 0

Views: 501

Answers (2)

Friedrich Große
Friedrich Große

Reputation: 2443

I especially like about go that its entire environment is particularly easy to setup just with wget and tar and without the need for any special permissions.

This is how I usually install (and update) go on any system:

$ wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz
$ tar -xzvf go1.6.2.linux-amd64.tar.gz
$ export GOROOT=$PWD/go
$ export PATH=$PWD/go/bin:$PATH
$ which go
/tmp/example/go/bin/go
$ go version
go version go1.6.2 linux/amd64

You can find the links to the tar archives here: https://golang.org/dl/

I like to download and extract each version I use to $HOME/Programs and then just set a symlink to point to the one I actually want to use:

ls -la /home/fgrosse/Programs | grep go
lrwxrwxrwx   1 fgrosse fgrosse    6 Mar  9 20:52 go -> go1.6.1
drwxr-xr-x  11 fgrosse fgrosse 4.0K Feb 17 21:47 go1.5.4
drwxr-xr-x  11 fgrosse fgrosse 4.0K Feb 17 21:47 go1.6
drwxrwxr-x  11 fgrosse fgrosse 4.0K Apr 23 19:58 go1.6.1
drwxrwxr-x  11 fgrosse fgrosse 4.0K Apr 23 19:58 go1.6.2
$ echo $GOROOT
/home/fgrosse/Programs/go

Upvotes: 1

Ben Darnell
Ben Darnell

Reputation: 22154

sudo removes environment variables by default, so the command running as root can't see the environment variable set as doug. In general you shouldn't need sudo to run go get, although some of the golang.org/x/tools packages are somewhat special. Try installing just the ones you need (without sudo) instead of using ...

Upvotes: 2

Related Questions