Managing multiple Go versions with direnv

Install direnv

$ brew install direnv

Add to your shell

Create .envrc

GO={path to go version you want to use}
# If you use Homebrew
# GO=/opt/homebrew/Cellar/go@1.20/1.20.7/bin/go
export GOROOT="$($GO env GOROOT)"

PATH_rm '/opt/homebrew/Cellar/go*' # remove other versions
PATH_add "$($GO env GOROOT)/bin"

# use version specific GOPATH
export GOPATH=$HOME/go1.20
export PATH=$PATH:$GOPATH/bin

Allow .envrc

$ direnv allow

or allow prefix by setting up ~/.config/direnv/direnv.toml

prefix = ["{path to your workspace}"]

CAUTION: This allows execution of .envrc in all sub-directories.