Skip to content

Instantly share code, notes, and snippets.

@rafiramadhana
Last active November 28, 2022 12:55
Show Gist options
  • Save rafiramadhana/5b7d7aec1a38c4b58f48f1fb2c17019e to your computer and use it in GitHub Desktop.
Save rafiramadhana/5b7d7aec1a38c4b58f48f1fb2c17019e to your computer and use it in GitHub Desktop.
go-switch

go-switch

Switching between Go version with ease

➜  ~ go-switch list
go1.13.15
go1.15
go1.16
go1.16.13
go1.17.1
go1.18.7
#!/bin/bash
go_version=$1
go_versions_path=""
go_symlink_path="/usr/bin/go"
if [[ "$go_version" == "" ]]; then
echo "Empty Go version, please use: go-switch [[Go version]]"
exit 1
fi
if [[ "$go_version" == "list" ]]; then
ls $go_versions_path | egrep 'go.'
exit 0
fi
# Check if version exists
if [[ -d "$go_versions_path/$go_version" ]]
then
: # no-op
else
echo "Go version of $go_version does not exist in $go_versions_path"
echo "Existing versions in $go_versions_path:"
echo $(ls $go_versions_path)
exit 1
fi
# Replace existing Go in $GOROOT
rm -rf $go_versions_path/go
cp -r $go_versions_path/$go_version $go_versions_path/go
# Delete existing soft link
rm -f $go_symlink_path
# Create soft link
ln -s $go_versions_path/$go_version/bin/go $go_symlink_path
updated_go_version=$(go version)
if [[ "$updated_go_version" == *"$go_version"* ]]
then
echo "Go version updated to: $go_version"
else
echo "Error when updating Go version"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment