Skip to content

Instantly share code, notes, and snippets.

@samson4649
Created March 30, 2022 23:22
Show Gist options
  • Save samson4649/f70641ab09ca706c57c46d52994c69f3 to your computer and use it in GitHub Desktop.
Save samson4649/f70641ab09ca706c57c46d52994c69f3 to your computer and use it in GitHub Desktop.
GoLang Latest Installer
#!/bin/bash
# check that root is running
if [ "${EUID}" -ne 0 ]; then
echo "This script needs to be run as root. Exiting..."
exit 1
fi
# get current version of go
current=$(go version | cut -d' ' -f3)
# get latest version filename from go.dev
filename=$(curl -sSL 'https://go.dev/dl/?mode=json' \
| jq '.[0]' \
| grep filename \
| grep linux-amd64 \
| cut -d'"' -f4)
# format latest version filename to get version only
target=$(echo ${filename} | sed -E 's/(.+)\.linux.*/\1/')
# check if latest version is already installed
if [ "${current}" = "${target}" ]; then
echo "Latest version already installed"
exit 0
fi
# move to tmp and download the latest release
cd /tmp && wget https://go.dev/dl/${filename}
# remove existing and install latest version (code from go.dev website)
rm -rf /usr/local/go \
&& tar -C /usr/local -xzf ${filename} \
&& echo "Successfully installed ${target}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment