Skip to content

Instantly share code, notes, and snippets.

@azlan
Last active May 4, 2023 02:45
Show Gist options
  • Save azlan/b2fadbd540522d490c9e2d868b50fcb6 to your computer and use it in GitHub Desktop.
Save azlan/b2fadbd540522d490c9e2d868b50fcb6 to your computer and use it in GitHub Desktop.
update go
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: ./update_go.sh version (example 1.20.4)"
exit 1
fi
GO_VERSION=$1
GO_ARCHIVE="go${GO_VERSION}.linux-amd64.tar.gz"
GO_DOWNLOAD_URL="https://go.dev/dl/${GO_ARCHIVE}"
TMP_DIR="/tmp"
# Download Go archive
echo "Downloading Go version ${GO_VERSION}..."
curl -L -o "${TMP_DIR}/${GO_ARCHIVE}" "${GO_DOWNLOAD_URL}"
# Verify the download
if [ $? -ne 0 ]; then
echo "Failed to download Go version ${GO_VERSION}. Please check the URL and try again."
exit 1
fi
# Remove the previous Go installation
echo "Removing previous Go installation..."
sudo rm -rf /usr/local/go
# Extract the downloaded archive to /usr/local
echo "Installing Go version ${GO_VERSION}..."
sudo tar -C /usr/local -xzf "${TMP_DIR}/${GO_ARCHIVE}"
# Remove the downloaded archive
rm "${TMP_DIR}/${GO_ARCHIVE}"
# Check the installed version
INSTALLED_VERSION=$(go version 2>/dev/null | awk '{print $3}' | tr -d 'go')
if [ "${INSTALLED_VERSION}" == "${GO_VERSION}" ]; then
echo "Go version ${GO_VERSION} successfully installed."
else
echo "Installation failed. Please try again."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment