Last active
August 8, 2024 10:21
-
-
Save dot-mike/b9eb608843268295b32238a7a48d1f89 to your computer and use it in GitHub Desktop.
This script downloads and installs a specific version of Go lang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This script downloads and installs a specific version of Go. | |
# Check if the version is provided | |
if [ -z "$1" ]; then | |
echo "Usage: ./update_go.sh version (example 1.20.4 or latest)" | |
exit 1 | |
fi | |
if [ "$1" == "latest" ]; then | |
GO_VERSION=$(curl -L -s https://golang.org/VERSION?m=text | head -n 1) | |
else | |
GO_VERSION=$1 | |
if [[ ! "$GO_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
echo "Invalid version number. Please provide a valid version number (example 1.20.4 or latest)." | |
exit 1 | |
fi | |
GO_VERSION="go${GO_VERSION}" | |
fi | |
GO_ARCHIVE="${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}..." | |
response=$(curl -L -s -w "%{http_code}" -o "${TMP_DIR}/${GO_ARCHIVE}" "${GO_DOWNLOAD_URL}") | |
if [ $response -ne 200 ]; 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}" | |
echo "Set the go path in .bashrc" | |
echo 'export GOPATH="$HOME/go";export PATH="$PATH:/usr/local/go/bin:$GOPATH/bin"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment