Skip to content

Instantly share code, notes, and snippets.

@kfcampbell
Created March 25, 2020 17:12
Show Gist options
  • Save kfcampbell/c9029888c7271594f6e1486885e73e6b to your computer and use it in GitHub Desktop.
Save kfcampbell/c9029888c7271594f6e1486885e73e6b to your computer and use it in GitHub Desktop.
Easily switch Go versions at the command-line.
# my amazing colleague @fatih showed me this. saving it here so i don't forget it.
function switchgo() {
version=$1
if [ -z $version ]; then
echo "Usage: switchgo [version]"
return
fi
if ! command -v "go$version" > /dev/null 2>&1; then
echo "version does not exist, downloading with commands: "
echo " go get golang.org/dl/go${version}"
echo " go${version} download"
echo ""
go get "golang.org/dl/go${version}"
go${version} download
fi
go_bin_path=$(command -v "go$version")
ln -sf "$go_bin_path" "$GOBIN/go"
echo "Switched to ${go_bin_path}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment