Mirza
Mirza

Reputation: 311

Cannot do any go command anymore

Before it happened, what I am doing is trying to use the dep to manage my golang code dependency. What I found right now is I cannot do any command with go, even if I try to uninstall it with brew by brew uninstall go and do brew install go again.

If I am doing a go env it will show like this:

$ go env
go: cannot find GOROOT directory: /usr/local/cellar/go/1.13.1/libexec

$ ls /usr/local/Cellar/go/1.13.8/libexec/
CONTRIBUTING.md SECURITY.md bin     lib     robots.txt
CONTRIBUTORS    VERSION     doc     misc        src
PATENTS     api     favicon.ico pkg     test

$ go version
go: cannot find GOROOT directory: /usr/local/cellar/go/1.13.1/libexec

$ go build
go: cannot find GOROOT directory: /usr/local/cellar/go/1.13.1/libexec

$ echo $GOPATH
/Users/mymac/go

$ echo $GOROOT

$

What should I do and check?

Upvotes: 13

Views: 29215

Answers (4)

Squareme
Squareme

Reputation: 89

With every update of Golang I always had an issue with JetBrains GoLand with error message similar to this one:

Cannot run program "/opt/homebrew/Cellar/go/1.22.3/libexec/bin/go" (in directory "/Users/..."): error=2, No such file or directory

Which always remind me that I have to update GOROOT path in ~/.zshrc from

export GOROOT=/opt/homebrew/Cellar/go/1.22.3/libexec

to whatever the latest version is!

Now I started using the following:

export GOROOT=$(brew --cellar golang)/$(ls -1 $(brew --cellar golang) | sort -r | head -n 1)/libexec

which basically gets the list of all installed go versions inside the base path /opt/homebrew/Cellar/go , sort by name and return the latest version and use that number (for example 1.22.4) inside GOROOT!

Now, with every brew update I have the latest version populated by .zshrc

Upvotes: 1

Kulvinder Singh
Kulvinder Singh

Reputation: 1

I used homebrew to install Go ver. 1.20, and above solutions worked for me with small tweaks.

Instead of this:

export GOROOT=/usr/local/opt/go/libexec

I had to use this:

export GOROOT=/opt/homebrew/opt/[email protected]/libexec

Basically search for libexec folder in your local using :

 which libexec 

and then use this path for setting GOROOT.

Upvotes: 0

Hons
Hons

Reputation: 4046

I personally use this for Homebrew

export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
# Homebrew
export GOROOT="$(brew --prefix golang)/libexec"
# Manual install
# export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

Blogpost

Upvotes: 49

paulsm4
paulsm4

Reputation: 121799

Try this:

https://gist.github.com/vsouza/77e6b20520d07652ed7d

# Set variables in .bashrc file

# don't forget to change your path correctly!
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

Of course, you'll need to change "$HOME/golang" and "/usr/local/opt/go: to your actual path names.


From OP:

finally i solve this, can you help to update your comment then i will set it as SOLVED.

i use

export GOROOT=/usr/local/Cellar/go/1.13.8/libexec/

instead of

GOROOT=/usr/local/opt/go/libexec

Upvotes: 5

Related Questions