Put this in your .bashrc/.zshrc
changego() {
if ! which $1 > /dev/null; then
echo "$1 is not installed. See this https://gist.github.com/afjoseph/90218a0cc753219cf5f07aaab28454c5"
return
fi
a="$($1 env GOROOT)"
sudo ln -sf "$a/bin/go" "$(brew --prefix)/bin/go"
}
Figure out your current Go version
go version
Let's say the output above was "1.20.3" Install it (yes, even if it was already installed) to the list of Go versions you have in your system.
go install golang.org/dl/go1.20.3@latest
go1.20.3 download
Now you have your version of Go installed with semantic versioning
Symlink it
changego go1.20.3
Running go version
should yield 1.20.3
Install another Go version, like 1.18.7
go install golang.org/dl/go1.18.7@latest
go1.18.7 download
Switch the Go version
changego go1.18.7
Running go version
now should yield 1.18.7
And you can switch back using changego 1.20.3
anytime