Last active
August 15, 2025 11:44
-
-
Save meysam81/cb706f51ec3fa301c6624a3c88595c1b to your computer and use it in GitHub Desktop.
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
| #!/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