Use the go install golang.org/dl/go<version>@latest command to install specific Go versions into your $GOPATH/bin directory, then switch between them using go<version> install or by running the binary directly.
# Install Go 1.21 and Go 1.22 side-by-side
go install golang.org/dl/go1.21@latest
go install golang.org/dl/go1.22@latest
# Use a specific version for a command
go1.21 version
go1.22 build ./...
This method installs each version as a separate executable (e.g., go1.21, go1.22) in your $GOPATH/bin, allowing you to run them simultaneously without overwriting your system Go installation.