-
-
Save bytedaring/3f740aa12b5c4c177d5e82f0bf27c965 to your computer and use it in GitHub Desktop.
A shell script for switching the version of Go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function switchgo() { | |
version=$1 | |
if [ -z $version ] || [ $# -ne 1 ]; then | |
echo "Usage: switchgo [version]" | |
return 1 | |
fi | |
if [ -z $GOBIN ]; then | |
echo "\$GOBIN is not set" | |
return 1 | |
fi | |
if ! command -v $GOBIN/go$version > /dev/null 2>&1; then | |
echo "Go $version doesn't exist, start downloading..." | |
if [ $(go version | grep -Eo "[1-9][0-9]+" | head -1) -ge 16 ]; then | |
go install golang.org/dl/go$version@latest | |
else | |
go get golang.org/dl/go$version | |
fi | |
if ! command -v $GOBIN/go$version > /dev/null 2>&1; then | |
return 1 | |
fi | |
$GOBIN/go$version download | |
fi | |
ln -sf $(command -v $GOBIN/go$version) $GOBIN/go | |
echo "Switched to Go $version" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment