Skip to content

Instantly share code, notes, and snippets.

@meysam81
Last active August 15, 2025 11:44
Show Gist options
  • Select an option

  • Save meysam81/cb706f51ec3fa301c6624a3c88595c1b to your computer and use it in GitHub Desktop.

Select an option

Save meysam81/cb706f51ec3fa301c6624a3c88595c1b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
if [ $# -eq 0 ]; then
echo "Usage: $0 <go_version>"
echo "Example: $0 1.25.0"
exit 1
fi
VERSION=$1
if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
echo "Error: Version must be in format X.Y.Z (e.g., 1.25.0)"
exit 1
fi
if [ -z "$GOROOT" ]; then
GOROOT="/usr/local/go"
echo "GOROOT not set, using default: $GOROOT"
fi
URL="https://go.dev/dl/go${VERSION}.linux-amd64.tar.gz"
FILENAME="go${VERSION}.linux-amd64.tar.gz"
echo "Downloading Go ${VERSION} from ${URL}..."
if ! wget -O "/tmp/${FILENAME}" "$URL"; then
echo "Error: Failed to download Go ${VERSION}"
echo "URL might be incorrect or version doesn't exist"
exit 1
fi
if [ -d "$GOROOT" ]; then
BACKUP_DIR="${GOROOT}_backup_$(date +%Y%m%d_%H%M%S)"
echo "Backing up current Go installation to ${BACKUP_DIR}..."
mv "$GOROOT" "$BACKUP_DIR"
fi
echo "Installing Go ${VERSION} to ${GOROOT}..."
tar -C "$(dirname "$GOROOT")" -xzf "/tmp/${FILENAME}"
if [ -x "$GOROOT/bin/go" ]; then
echo "Installation successful!"
echo "Go version: $($GOROOT/bin/go version)"
else
echo "Error: Installation verification failed"
exit 1
fi
rm "/tmp/${FILENAME}"
echo "Done! Go ${VERSION} has been installed to ${GOROOT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment