12

I installed Go 1.4 in Mac OS X. Previously I had Go 1.0. I set the GOROOT and PATH as follows,

Dineshs-MacBook-Air:go-cassandra Dany$ which go
/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin 

Go is installed in '/usr/local/go/bin/go'. And I set the GOPATH as my project src directory. I am able to run go code inside my directory. But when I try to install gocql I am getting error.

Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath

Could anyone help me on this? Thank you

EDIT 1: @VonC I tried the other option as well. I changed the GOROOT to the directory where go is installed. But it didn't help. And I changed the GOPATH.

Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin
Dineshs-MacBook-Air:go-cassandra Dany$ export GOPATH=/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
Password:
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath
Dineshs-MacBook-Air:go-cassandra Dany$ echo $GOPATH
/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ ls
bin pkg src
Dineshs-MacBook-Air:go-cassandra Dany$
Dany
  • 2,144
  • 4
  • 38
  • 56

3 Answers3

36

Notes:

GOROOT should reference a folder (where go is installed), not the go executable itself

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin 

As Dave mentions in the comments, you should not have to set GOROOT at all in your case.
See the article You don’t need to set GOROOT, really.

GOPATH should reference a folder under which you will find src, pkg and bin. (it should not reference directly the src folder):
See "How to Write Go Code - Workspace"

Regarding the GOPATH:

  • try and set it in your ~/.bashrc (using export).
  • check that your current shell is a bash (and not another one like fish)
  • check the output of go env.

Don't do a sudo go get, as the environment variable used for sudo (root) wouldn't be the same as the current user:

go get github.com/gocql/gocql

(or you would need to do a sudo -E bash -c 'go get github.com/gocql/gocql', but I suspect you don't need root here)

See sudo caveat:

Any variables added to these locations will not be reflected when invoking them with a sudo command, as sudo has a default policy of resetting the Environment and setting a secure path (this behavior is defined in /etc/sudoers)

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • I tried. But still facing the same issue. I edited the question with the current GOPATH and GOROOT – Dany Feb 26 '15 at 12:00
  • Thank you. It worked without sudo. Could you explain me how it works. ? I am new to mac. – Dany Feb 26 '15 at 12:21
  • 2
    [You don’t need to set GOROOT](http://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really) and you normally don't want to. – Dave C Feb 26 '15 at 14:34
  • @DaveC I have seen that before. I always set it anyway because I always unzip my go installation package in a non-conventional path (I am that "Windows user using the zip binary downloaded from the golang.org website"). I have included the link to the article in the answer for more visibility. – VonC Feb 26 '15 at 14:36
  • @VonC, that's one of the exceptions specifically mentioned in the article. I set it too (for other reasons, like I find it convenient to do things like `grep $GOROOT/src/…` from time to time). The idea though is don't start out setting it or advising others to do so unless there is a specific need. – Dave C Feb 26 '15 at 14:40
0

I face many issues to set the GOROOT and GOPATH in MacOS, and the only solution that worked for me, is the below one:

Add the following code into your ~/.bashrc or ~/.profile if you use Oh My Zsh ~/.zshrc

export GOPATH=$HOME/go
export GOROOT=/usr/local/go

After that, run the following command in your terminal:

source ~/.profile

If you have any different shell, replace it in the place of .profile.

Info: $HOME variable will point to your current user's directory.

Info 2: /usr/local/go is where all users, the go files, and bin folder exists.

To Set Custom GOPATH as working directory:

To store your projects of GoLang in different folders, you can set the custom path. Here is my custom GOPATH, which points to my goWorkSpace folder in my admin/Documents folder.

export GOPATH=$HOME/Documents/goWorkSpace
Riyaz Khan
  • 933
  • 4
  • 14
0

You will need to inform to Go the location of your workspace. In this example we gonna use $HOME/dev/go-workspace.

Then you need to know if your mac have zsh or bash configured as shell.

The file ~/.zshrc is used for zsh shell. The zsh shell was introduced in OS Big Sur.

The ~/.bashrc is the bash shell used in previews OS version, the same for linux users.

1: Add those lines to export the required variables in your ~/.zsh or ~./bashrc depending on your shell.

For go installed from original pkg download from https://golang.org/doc/install

export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

For Go installed from home brew. (brew update and brew install golang)

export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

2: run

# source ~/.zshrc 

or

source ~./bashrc 

to update your $PATH according with the new variables inserted in step #2

3: Then create your the workspace directories:

$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin

4: create a test.go, the hello world

package main
import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

Run your program by executing:

$ go run test.go

If you wish to compile it and move it to $GOPATH/bin, then run:

$ go install test.go

Since we have $GOPATH/bin added to your $PATH, you can run your program from anywhere just typing test :

$ test

If everything is right the output will be :

hello, world
Cassio Seffrin
  • 2,916
  • 28
  • 34