Golang-related envs prevent latest golang from running
RaySuhyunLee opened this issue ยท 6 comments
Hi, thank you for making such a great dotfile set.
Problem
These lines seem to prevent the recent release of golang from working.
export GOROOT=$HOME/.go
export GOPATH=$GOROOT/packages
path=( $path $GOROOT/bin $GOPATH/bin )
Every command (e.g., golang version, golang env, golang run, etc...) fails with the following error.
go: cannot find GOROOT directory: /Users/ray.l/.go
According to this (and other similar SO articles) it seems like setting GOROOT manually is not recommended, unless when using multiple versions of golang at the same time. Maybe these variables are for older version of golang?
Solution
When removing the above lines, golang works fine.
Context
- golang version: 1.20.2 darwin/arm64
- installation method: official website.
Hello, thank you very much for such a detailed issue. I think the default GOROOT would be $HOME/go
, which I don't like personally because it is not a hidden directory. Anyway I'd need to add $GOROOT/bin
to $PATH
, but knowing the value of $GOROOT
explicitly would be good (let me know if there's any better way to configure $PATH
variable w.r.t the current go installation).
I think the line that's probably missing is
mkdir -p $GOROOT
The latest golang 1.20.2 works fine in my environments (without those lines being removed). Can you try adding the above in ~/.zsh/zsh.d/envs.zsh
to eliminiate the error?
Thank you for the response.
When I make $HOME/.go
directory and run go build
command, the following error appears:
main.go:4:5: package encoding/json is not in GOROOT (/Users/ray.l/.go/src/encoding/json)
main.go:5:5: package net/http is not in GOROOT (/Users/ray.l/.go/src/net/http)
package example.com/main: cannot find package
which complains about the missing files. It makes sense because the new directory (~/.go
) that I've just made is empty.
Actually, I found out that the official go installer installs under the following path
which go
/usr/local/go/bin/go
When I set GOROOT
to /usr/local/go
, go command works fine again. But I think this is not necessary because go-related envs are automatically configured by the latest go installer and don't need to be configured manually ๐
When I remove GOROOT
and GOPATH
settings from ~/.zsh/zsh.d/envs.zsh
, go still finds its path properly.
echo $GOROOT
(empty line)
echo $GOPATH
(empty line)
go env
...
GOPATH="/Users/ray.l/go" # this directory actually doesn't exist, but go works without problem
...
GOROOT="/usr/local/go"
...
What method did you use to install go?
which complains about the missing files. It makes sense because the new directory (~/.go) that I've just made is empty.
I think you will need to install deps (go install
) if GOROOT
has changed.
Since Go 1.11+, GOPATH is no longer recommended, but I'm still confused how go-installed binaries can be found in $PATH
. I still prefer not having GOROOT
or GOPATH
resolved to $HOME/go
(the default GOPATH
).
Without those lines, go env
prints
GOROOT="/opt/homebrew/Cellar/go/1.20.2/libexec"
GOPATH="/Users/wookayin/go"
and go-installed binaries exist in $GOPATH/bin
. So it looks like that I can just remove the GOROOT
variable.
BTW I used homebrew to install go, but I don't think things will be different than when installed via official installer. I think back in 2018, I had a custom go installation locally (for linux machines) on $HOME/.go
; and GOPATH
went to $HOME/.go/packages
. Now that this is somewhat deprecated setup, we can move away from this by not setting GOROOT
which should be a right fix as you suggested.
This is the suggested fix, using a non-default GOPATH
. Would this work for you?
# GO {{{
-export GOROOT=$HOME/.go
-export GOPATH=$GOROOT/packages
-path=( $path $GOROOT/bin $GOPATH/bin )
+export GOPATH=$HOME/.go
+path=( $path $GOPATH/bin )
+mkdir -p $GOPATH
# }}}
I still prefer not having GOROOT or GOPATH resolved to $HOME/go (the default GOPATH).
Aha. Now I understand why we need explicit GOPATH. I also agree that ~/.go
is much better than ~/go
(default path).
After trying your suggested fix, now everything works like charm!
Thank you very much ๐