Skip to content

Instantly share code, notes, and snippets.

@vimiix
Last active June 28, 2023 15:06
Show Gist options
  • Save vimiix/0927fdfbea926e869a2c631db9eeae8b to your computer and use it in GitHub Desktop.
Save vimiix/0927fdfbea926e869a2c631db9eeae8b to your computer and use it in GitHub Desktop.
Shell functions for managing golang multiple versions
####### GOLANG VERSION MANAGE FUNCTIONS ######
# ref: https://go.dev/doc/manage-install
function goinstall() {
echo "Downloading go$1 ..."
go install golang.org/dl/go$1@latest && go$1 download
}
function gouse() {
gopath=$(go env GOPATH)
if test -x ${gopath}/bin/go$1; then
rm -f ${gopath}/bin/go
echo "Relink go with go$1 ..."
ln -s ${gopath}/bin/go$1 ${gopath}/bin/go
echo "Done"
else
echo "Version $1 not installed"
fi
}
function golist() {
current=$(go version | awk '{print $3}' | cut -c3-)
for v in $(ls $(go env GOPATH)/bin | grep -E 'go(\d.*)' | cut -c3-);
do
if [ $v = $current ]; then
echo "$v (⇦ current)"
else
echo $v
fi
done
}
function gouninstall() {
current=$(go version | awk '{print $3}' | cut -c3-)
if [ $1 = $current ]; then
echo "version $1 is actived, please change to another version first"
return
fi
echo "Removing binary..."
rm -f $(go env GOPATH)/bin/go$1
echo "Removing sdk ..."
rm -r ~/sdk/go$1
echo "Done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment