Last active
February 23, 2025 08:41
-
-
Save mdsohelmia/fa42d8052f581a3d521a3a811b3f4ddd to your computer and use it in GitHub Desktop.
install-go
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 # Exit on error | |
| # Detect OS (linux, darwin, windows) | |
| OS_TYPE=$(uname | tr '[:upper:]' '[:lower:]') | |
| # Detect Architecture (amd64, arm64, 386) | |
| ARCH_TYPE=$(uname -m) | |
| case "$ARCH_TYPE" in | |
| x86_64) ARCH="amd64" ;; | |
| arm64 | aarch64) ARCH="arm64" ;; | |
| i386 | i686) ARCH="386" ;; | |
| *) echo "โ Unsupported architecture: $ARCH_TYPE"; exit 1 ;; | |
| esac | |
| # Detect shell (bash or zsh) | |
| SHELL_RC="$HOME/.bashrc" | |
| [ -n "$ZSH_VERSION" ] && SHELL_RC="$HOME/.zshrc" | |
| # Get the latest Go version | |
| GO_VERSION=$(curl -s 'https://go.dev/VERSION?m=text' | awk -F 'go' '/^go/ {print $2}') | |
| GO_TAR="go${GO_VERSION}.${OS_TYPE}-${ARCH}.tar.gz" | |
| DOWNLOAD_URL="https://go.dev/dl/${GO_TAR}" | |
| INSTALL_DIR="/usr/local" | |
| echo "๐น Detected OS: $OS_TYPE" | |
| echo "๐น Detected Architecture: $ARCH" | |
| echo "๐น Latest Go Version: $GO_VERSION" | |
| echo "๐น Downloading: $DOWNLOAD_URL" | |
| echo "๐น Detected Shell: $SHELL_RC" | |
| # Download Go tarball | |
| curl -LO "$DOWNLOAD_URL" | |
| # Validate the tarball | |
| if ! tar -tzf "$GO_TAR" > /dev/null; then | |
| echo "โ Downloaded tarball is corrupted or incorrect." | |
| exit 1 | |
| fi | |
| # Remove any existing Go installation | |
| sudo rm -rf "$INSTALL_DIR/go" | |
| # Ensure the parent directory exists | |
| sudo mkdir -p "$INSTALL_DIR/go" | |
| # Extract Go | |
| sudo tar -C "$INSTALL_DIR" -xzf "$GO_TAR" | |
| # Clean up | |
| rm "$GO_TAR" | |
| # Update PATH | |
| GO_PATH_ENTRY="export PATH=$INSTALL_DIR/go/bin:\$PATH" | |
| if ! grep -qxF "$GO_PATH_ENTRY" "$SHELL_RC"; then | |
| echo "$GO_PATH_ENTRY" >> "$SHELL_RC" | |
| echo "โ PATH updated in $SHELL_RC" | |
| else | |
| echo "๐น PATH already exists in $SHELL_RC" | |
| fi | |
| # Apply changes | |
| if [ -f "$SHELL_RC" ]; then | |
| # shellcheck source=/dev/null | |
| source "$SHELL_RC" | |
| fi | |
| # Verify | |
| go version | |
| echo "โ Golang $GO_VERSION installed successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment