10

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?

Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49
Mirza
  • 281
  • 1
  • 4
  • 14
  • Do not install Go via brew. Use the official Go distribution from golang.org. And do not use dep. – Volker Feb 26 '20 at 07:50

2 Answers2

35

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

Hons
  • 3,804
  • 3
  • 32
  • 50
6

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

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • Well, i don't think GOPATH should point to GO binary path. GOPATH is where maintanable sour source code is residing. – Mirza Feb 26 '20 at 04:32
  • Fair enough. See my revised comments – FoggyDay Feb 26 '20 at 04:40
  • 2
    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. – Mirza Feb 26 '20 at 04:45