Skip to content

Instantly share code, notes, and snippets.

@joanna-liana
Last active August 30, 2023 15:58
Show Gist options
  • Save joanna-liana/648e2c9fd6b1ad3e7ca073681421e0c5 to your computer and use it in GitHub Desktop.
Save joanna-liana/648e2c9fd6b1ad3e7ca073681421e0c5 to your computer and use it in GitHub Desktop.
Easy-peasy Go update; a bash script using the latest available version (default) or a specific archive name; ZSH is assumed to be the default shell.
#!/usr/bin/env bash
# Script adapted from https://golangcode.com/updating-go-on-ubuntu/
set -o errexit
main () {
if ! [[ -z "$1" ]]
then
printf "❕ To update Go to the latest version, you do not need to provide any arguments.\nAlternatively, provide just the name of the specific Linux archive, e.g. go1.19.linux-amd64.tar.gz\n\n"
fi
# -s to silence output, -L to deal with Moved Permanent HTTP status
archiveName="$(curl -Ls https://golang.org/VERSION?m=text | head -n1).linux-amd64.tar.gz"
if [[ $# -ne 1 ]]
then
printf "No valid archive name provided. Using the latest available archive: $archiveName\n\n"
else
archiveName=$1
fi
shouldAddToPath=false
if ! command -v go &> /dev/null
then
printf "⚠ Go not installed"
shouldAddToPath=true
else
goVersion=$(go version)
printf "Removing $goVersion\n"
sudo rm -r /usr/local/go/
fi
printf "\n\n⏳ Downloading and installing version $archiveName\n\n"
wget https://dl.google.com/go/$archiveName
sudo tar -C /usr/local -xzf $archiveName
if [[ $shouldAddToPath = true ]]
then
export PATH=$PATH:/usr/local/go/bin
printf "\nexport PATH=\$PATH:/usr/local/go/bin\n" >> ~/.zshrc
printf "\n\nℹ Added go to PATH (zsh)\n\n"
printf "❗❗❗ To start using Go, OPEN A NEW TERMINAL TAB or run \"source ~/.zshrc\"\n"
fi
updatedGoVersion=$(go version)
printf "\n🎉 Success! Installed $updatedGoVersion\n"
rm $archiveName
printf "\n🧹 Cleaned up installation artefacts\n"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment