Skip to content

Instantly share code, notes, and snippets.

@afjoseph
Last active April 16, 2023 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afjoseph/90218a0cc753219cf5f07aaab28454c5 to your computer and use it in GitHub Desktop.
Save afjoseph/90218a0cc753219cf5f07aaab28454c5 to your computer and use it in GitHub Desktop.
Manage different Go versions

Managing Different Go Versions on an OSX machine

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment