Skip to content

Instantly share code, notes, and snippets.

@hitalos
Last active December 15, 2023 05:58
Show Gist options
  • Save hitalos/1165c8ddfa6d5002b57f1cfe12a15ebd to your computer and use it in GitHub Desktop.
Save hitalos/1165c8ddfa6d5002b57f1cfe12a15ebd to your computer and use it in GitHub Desktop.
Update Golang to latest version
#!/bin/bash
source <(go env)
INFO=$(curl -sf 'https://go.dev/dl/?mode=json')
[[ "$?" -ne "0" ]] && echo "Error getting latest info" && exit 1
FILEHASH=$(jq -r ".[0].files[] | select(.arch==\"$GOARCH\" and .os==\"$GOOS\") | \"\(.sha256) \(.filename)\"" <<< "$INFO")
FILENAME=${FILEHASH#* }
echo "Downloading $FILENAME"
curl --progress-bar -C - -fsLO "https://go.dev/dl/$FILENAME"
RESULT=$?
[[ "$RESULT" -eq "0" ]] || [[ "$RESULT" -eq "22" ]] && \
sha256sum --check --quiet <<< "$FILEHASH" && \
sudo rm -rf "$GOROOT" && \
sudo tar -C "$(dirname \"$GOROOT\")" -xzf "$FILENAME" && \
rm -f "$FILENAME" && \
echo OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment