Skip to content

Instantly share code, notes, and snippets.

@tMinamiii
Created October 4, 2022 15:37
Show Gist options
  • Save tMinamiii/57f6cd88684499853ba308c1c5188051 to your computer and use it in GitHub Desktop.
Save tMinamiii/57f6cd88684499853ba308c1c5188051 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -Ceu
CURRENT_GO_VERSION=$(go version | cut -f 3 -d " ")
LATEST_GO_VERSION=$(curl -s 'https://go.dev/dl/?mode=json' | jq -r '[.[]][0].version')
if [ "${CURRENT_GO_VERSION}" = "${LATEST_GO_VERSION}" ]; then
echo "latest version already installed. -- ${CURRENT_GO_VERSION}"
exit 0
fi
OS="$(uname)"
ARCH="$(uname -m)"
echo "Latest go version is --- ${LATEST_GO_VERSION} ---"
echo "OS: ${OS}"
echo "Arch: ${ARCH}"
if [ "${OS}" = "Darwin" ]; then
GOPKG=""
if [ "${ARCH}" = "x86_64" ]; then
GOPKG="${LATEST_GO_VERSION}.darwin-amd64.pkg"
elif echo "${ARCH}" | grep -sq "arm"; then
GOPKG="${LATEST_GO_VERSION}.darwin-arm64.pkg"
fi
[ "$GOPKG" = "" ] && exit
sudo curl -OL --progress-bar "https://go.dev/dl/${GOPKG}"
trap 'sudo rm ${GOPKG}' EXIT
sudo installer -pkg "${GOPKG}" -target /usr/local/go
echo "Install completed !!"
/usr/local/go/bin/go version
elif [ "${OS}" = "Linux" ]; then
GOGZ=""
if [ "${ARCH}" = "x86_64" ]; then
GOGZ="${LATEST_GO_VERSION}.linux-amd64.tar.gz"
elif ${ARCH} | grep -sq "arm"; then
GOGZ="${LATEST_GO_VERSION}.linux-arm64.tar.gz"
fi
[ "$GOGZ" = "" ] && exit
trap 'sudo rm ${GOGZ}' EXIT
sudo curl -OL --progress-bar "https://go.dev/dl/${GOGZ}" && \
sudo rm -rf /usr/local/go && \
sudo tar -C /usr/local -xzf "${GOGZ}"
echo "Install completed !!"
/usr/local/go/bin/go version
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment