Skip to content

Instantly share code, notes, and snippets.

@bytedaring
Forked from aofei/switchgo.sh
Created October 9, 2021 06:15
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 bytedaring/3f740aa12b5c4c177d5e82f0bf27c965 to your computer and use it in GitHub Desktop.
Save bytedaring/3f740aa12b5c4c177d5e82f0bf27c965 to your computer and use it in GitHub Desktop.
A shell script for switching the version of Go.
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