Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set GOPATH in Mac OS X 10.10

Tags:

go

gocql

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$
like image 394
Dany Avatar asked Feb 26 '15 11:02

Dany


People also ask

How do I find Gopath on Mac?

GOPATH is a variable that defines the root of your workspace. By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS, %USERPROFILE%/go for Windows). GOPATH stores your code base and all the files that are necessary for your development.

How do I find Goroot and Gopath on Mac?

The correct values for GOPATH and GOROOT However, GOROOT is the place where the Go binary distributions assume they will be installed (so in Linux distributions, it's normally in /usr/local/go , but for MacOS, the Go and the tools are installed in /usr/local/Cellar/go/1.13/x. xx.

What Gopath should be set to?

You generally should not set GOROOT explicitly. The go command identifies the appropriate GOROOT automatically based on its own directory location. GOPATH defaults to $HOME/go . You only need to set it explicitly if you want to put it somewhere else.


2 Answers

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)

like image 122
VonC Avatar answered Sep 18 '22 18:09

VonC


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
like image 38
Cassio Seffrin Avatar answered Sep 17 '22 18:09

Cassio Seffrin