compile: version "go1.19.5" does not match go tool version "go1.20.5”のメモ

久々に個人PCのGoのソースを実行しようと思ったら、タイトルのエラーが出た。1.20に上げてそのままんだったぽい

原因

パスが通っているgoのパスとGOROOTのパスが一致していなかった

❯ go env GOROOT
/usr/local/go

❯ echo $GOROOT
/usr/local/go

#GOROOTは1.19.5
❯ ll /usr/local | grep go
drwxr-xr-x 10 root root      4096 Jan 10  2023 go
-rw-r--r--  1 sho  sho  148949578 Jan 23  2023 go1.19.5.linux-amd64.tar.gz

-------------

❯ which go
/usr/bin/go

#こっちは1.20.5
❯ go version
go version go1.20.5 linux/amd64

対応

.zshrcで設定していてた環境変数GOROOTを削除。これでコンパイルエラーは出なくなった。

export GOROOT=/usr/local/go

変更後

❯ go env GOROOT
/usr/lib/go-1.20

❯ echo $GOROOT

❯ which go
/usr/bin/go

❯ go version
go version go1.20.5 linux/amd64

GOROOTとGOPATHの違い

GOROOTはgoのインストールパスを指定する。基本的には変える必要なかった。1.19と1.20を共存させたかったらここいじって切り替えるのがいいのか?

GOROOT is a variable that defines where your Go SDK is located. You do not need to change this variable, unless you plan to use different Go versions.

GOPATHはワークスペースのパス。デフォルトは~/go

GOPATH is a variable that defines the root of your workspace. By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS%USERPROFILE%/go for Windows).