Reputation: 2802
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$
Upvotes: 25
Views: 76811
Reputation: 3228
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
Upvotes: 0
Reputation: 13181
macos terminal:
brew install go
edit ~/.zshenv
export GOROOT="/usr/local/Cellar/go/1.16.6/libexec"
export GOPATH="/Users/Shared/Development/go-workspace"
export PATH="$PATH:$GOPATH/bin"
Think of the GOPATH in terms of a configured workspace for the environment. GOROOT is the install point for the go language. Homebrew will symlink the bin commands to your /usr/local/bin. Add the $GOPATH to your PATH in order to find any go programs that you later install.
Upvotes: 4
Reputation: 6920
You're setting $PATH
incorrectly. $GOROOT
is set and added to the $PATH
during installation. So neither:
export GOROOT=/usr/local/go/bin/go
export PATH=$PATH:$GOROOT/bin
nor:
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
is needed because $GOROOT
will be different based on how you've installed Go - Homebrew, go.dev, etc.
You just need to set $GOPATH
.
If you find XDG Base Directory Specification useful (this will keep your $HOME
clean), you could:
Create a user-specific data directory
mkdir -p $HOME/.local/share/go
Set $GOPATH
in .zshenv
export GOPATH="$HOME/.local/share/go"
export PATH="$PATH:$GOPATH/bin"
Refresh environment variables
source .zshenv
# or quit and re-open the shell
Install Go package
go install golang.org/x/tools/gopls@latest
Verify version of package
which gopls
Upvotes: 0
Reputation: 8550
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 macOS Catalina.
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
Upvotes: 7
Reputation: 1323203
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
:
~/.bashrc
(using export
).fish
)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, assudo
has a default policy of resetting the Environment and setting a secure path (this behavior is defined in/etc/sudoers
)
Upvotes: 59