Skip to content

Instantly share code, notes, and snippets.

@fatih
Last active December 31, 2019 13:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fatih/11d752582c5bd508ffe18a2df57c6424 to your computer and use it in GitHub Desktop.
Save fatih/11d752582c5bd508ffe18a2df57c6424 to your computer and use it in GitHub Desktop.
Switch between go version using https://github.com/golang/dl
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, download with: "
echo " go get golang.org/dl/go${version}"
echo " go${version} download"
return
fi
go_bin_path=$(command -v "go$version")
ln -sf "$go_bin_path" "$GOBIN/go"
echo "Switched to ${go_bin_path}"
}
@abstractj
Copy link

@fatih thanks for sharing this, that's very helpful. I had to do some minor changes to make it work:

if ! command -v "go$version" > /dev/null 2>&1; then
    echo "version does not exist, download with: "
    go get golang.org/dl/${version}
    ${version} download
    return
  fi

Sharing just in case others find the same issue.

@fatih
Copy link
Author

fatih commented Sep 26, 2019

Thanks for the tip @abstractj. Note that the download link is fixed, it should be now golang.org/dl/go${version}" not go get golang.org/dl/${version}".

I explicitly didn't run them because I sometimes write something wrong and didn't wanted it to download automatically, such as switchgo 1.14.

@abstractj
Copy link

@fatih cool and thanks for that. Keep sharing, I learn a lot from you :)

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