Skip to content

Instantly share code, notes, and snippets.

@theaog
Last active November 10, 2023 14:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theaog/e8923142320f57d2157dcb28b5cc6b0d to your computer and use it in GitHub Desktop.
Save theaog/e8923142320f57d2157dcb28b5cc6b0d to your computer and use it in GitHub Desktop.
update-golang.sh
#!/usr/bin/env bash
set -e
tmp=$(mktemp -d)
pushd "$tmp" || exit 1
function cleanup {
popd || exit 1
rm -rf "$tmp"
}
trap cleanup EXIT
version=$(go version |cut -d' ' -f3)
release=$(wget -qO- "https://golang.org/VERSION?m=text"| grep go)
release_file="${release}.linux-amd64.tar.gz"
if [[ $version == "$release" ]]; then
echo "local Go version ${release} is the latest."
exit 0
else
echo "local Go version ${version}, new release ${release} is available."
fi
echo "Downloading https://go.dev/dl/$release_file ..."
curl -OL https://go.dev/dl/"$release_file"
goroot=$(go env |grep GOROOT |cut -d'=' -f2 |tr -d '"')
echo "removing folder $goroot"
sudo rm -rf "$goroot"
sudo tar -C "${goroot//go}" -xzf "$release_file"
version=$(go version |cut -d' ' -f3)
echo "local Go version is $version (latest)"
@theaog
Copy link
Author

theaog commented Jun 8, 2023

if you want to add to a cronjob:

# taking a backup of current crons
crontab -l > crons
# adding new cron
echo "@weekly $HOME/update-golang.sh" >> crons
crontab crons
rm crons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment