Cannot do any go command anymore
Asked Answered
A

4

12

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?

Aguila answered 26/2, 2020 at 4:23 Comment(1)
Do not install Go via brew. Use the official Go distribution from golang.org. And do not use dep.Lascar
D
5

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

Diapophysis answered 26/2, 2020 at 4:29 Comment(3)
Well, i don't think GOPATH should point to GO binary path. GOPATH is where maintanable sour source code is residing.Aguila
Fair enough. See my revised commentsDiapophysis
finally i solve this, can you help to update your comment? i use export GOROOT=/usr/local/Cellar/go/1.13.8/libexec/ instead of GOROOT=/usr/local/opt/go/libexec then i will set it as SOLVED.Aguila
D
47

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

Duckpin answered 10/5, 2021 at 7:37 Comment(3)
export GOROOT="$(brew --prefix golang)/libexec" saved my day.Headstall
worked perfectly. Thanks.Nerveracking
Just dropped here to say this works perfectly! Thanks.Ferdinande
D
5

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

Diapophysis answered 26/2, 2020 at 4:29 Comment(3)
Well, i don't think GOPATH should point to GO binary path. GOPATH is where maintanable sour source code is residing.Aguila
Fair enough. See my revised commentsDiapophysis
finally i solve this, can you help to update your comment? i use export GOROOT=/usr/local/Cellar/go/1.13.8/libexec/ instead of GOROOT=/usr/local/opt/go/libexec then i will set it as SOLVED.Aguila
T
0

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.

Thibaud answered 29/4 at 13:15 Comment(0)
E
0

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

Exportation answered 18/6 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.