Skip to content

Instantly share code, notes, and snippets.

@mdsohelmia
Last active February 23, 2025 08:41
Show Gist options
  • Save mdsohelmia/fa42d8052f581a3d521a3a811b3f4ddd to your computer and use it in GitHub Desktop.
Save mdsohelmia/fa42d8052f581a3d521a3a811b3f4ddd to your computer and use it in GitHub Desktop.

Revisions

  1. mdsohelmia revised this gist Feb 23, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions install-go.sh
    Original file line number Diff line number Diff line change
    @@ -64,10 +64,10 @@ fi
    # Apply changes
    if [ -f "$SHELL_RC" ]; then
    # shellcheck source=/dev/null
    . "$SHELL_RC"
    source "$SHELL_RC"
    fi

    # Verify
    go version

    echo "✅ Golang $GO_VERSION installed successfully!"
    echo "✅ Golang $GO_VERSION installed successfully!"
  2. mdsohelmia revised this gist Feb 23, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-go.sh
    Original file line number Diff line number Diff line change
    @@ -70,4 +70,4 @@ fi
    # Verify
    go version

    echo "✅ Golang $GO_VERSION installed successfully!"
    echo "✅ Golang $GO_VERSION installed successfully!"
  3. mdsohelmia revised this gist Feb 23, 2025. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion install-go.sh
    Original file line number Diff line number Diff line change
    @@ -43,6 +43,9 @@ 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"

    @@ -67,4 +70,4 @@ fi
    # Verify
    go version

    echo "✅ Golang $GO_VERSION installed successfully!"
    echo "✅ Golang $GO_VERSION installed successfully!"
  4. mdsohelmia revised this gist Feb 23, 2025. 1 changed file with 25 additions and 31 deletions.
    56 changes: 25 additions & 31 deletions install-go.sh
    Original file line number Diff line number Diff line change
    @@ -5,72 +5,66 @@ 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) ARCH="arm64" ;;
    aarch64) ARCH="arm64" ;; # Some ARM systems use aarch64
    arm64 | aarch64) ARCH="arm64" ;;
    i386 | i686) ARCH="386" ;;
    *) echo "❌ Unsupported architecture: $ARCH_TYPE"; exit 1 ;;
    esac


    # Detect shell (bash or zsh)
    SHELL_RC=""
    SHELL_RC="$HOME/.bashrc"
    [ -n "$ZSH_VERSION" ] && SHELL_RC="$HOME/.zshrc"

    if [ "$SHELL" = "/bin/zsh" ] || [ "$SHELL" = "/usr/bin/zsh" ]; then
    SHELL_RC="$HOME/.zshrc"
    elif [ "$SHELL" = "/bin/bash" ] || [ "$SHELL" = "/usr/bin/bash" ]; then
    SHELL_RC="$HOME/.bashrc"
    else
    SHELL_RC="$HOME/.profile" # Default fallback
    fi


    # Get the latest Go version dynamically
    # Get the latest Go version
    GO_VERSION=$(curl -s 'https://go.dev/VERSION?m=text' | awk -F 'go' '/^go/ {print $2}')

    # Construct download URL
    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"
    echo "🔹 Detected Shell: $SHELL_RC"


    # Download the latest Go tarball
    # Download Go tarball
    curl -LO "$DOWNLOAD_URL"

    # Remove any existing Go installation
    sudo rm -rf /usr/local/go
    # 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"

    # Extract new Go installation
    # Extract Go
    sudo tar -C "$INSTALL_DIR" -xzf "$GO_TAR"

    # Clean up downloaded file
    # Clean up
    rm "$GO_TAR"

    # Define the Go PATH entry
    # Update PATH
    GO_PATH_ENTRY="export PATH=$INSTALL_DIR/go/bin:\$PATH"

    # Check if the PATH entry already exists in the shell profile
    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, skipping update."
    echo "🔹 PATH already exists in $SHELL_RC"
    fi

    # Apply changes to the current session
    # Apply changes to the current session safely (ShellCheck compliant)
    # Apply changes
    if [ -f "$SHELL_RC" ]; then
    # shellcheck source=/dev/null
    source "$SHELL_RC"
    . "$SHELL_RC"
    fi

    # Verify
    go version

    echo "✅ Golang $GO_VERSION installed successfully!"
  5. mdsohelmia renamed this gist Feb 23, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. mdsohelmia created this gist Feb 23, 2025.
    76 changes: 76 additions & 0 deletions sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/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) ARCH="arm64" ;;
    aarch64) ARCH="arm64" ;; # Some ARM systems use aarch64
    i386 | i686) ARCH="386" ;;
    *) echo "❌ Unsupported architecture: $ARCH_TYPE"; exit 1 ;;
    esac


    # Detect shell (bash or zsh)
    SHELL_RC=""

    if [ "$SHELL" = "/bin/zsh" ] || [ "$SHELL" = "/usr/bin/zsh" ]; then
    SHELL_RC="$HOME/.zshrc"
    elif [ "$SHELL" = "/bin/bash" ] || [ "$SHELL" = "/usr/bin/bash" ]; then
    SHELL_RC="$HOME/.bashrc"
    else
    SHELL_RC="$HOME/.profile" # Default fallback
    fi


    # Get the latest Go version dynamically
    GO_VERSION=$(curl -s 'https://go.dev/VERSION?m=text' | awk -F 'go' '/^go/ {print $2}')

    # Construct download URL
    GO_TAR="go${GO_VERSION}.${OS_TYPE}-${ARCH}.tar.gz"
    DOWNLOAD_URL="https://go.dev/dl/${GO_TAR}"


    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 the latest Go tarball
    curl -LO "$DOWNLOAD_URL"

    # Remove any existing Go installation
    sudo rm -rf /usr/local/go


    # Extract new Go installation
    sudo tar -C "$INSTALL_DIR" -xzf "$GO_TAR"

    # Clean up downloaded file
    rm "$GO_TAR"

    # Define the Go PATH entry
    GO_PATH_ENTRY="export PATH=$INSTALL_DIR/go/bin:\$PATH"

    # Check if the PATH entry already exists in the shell profile
    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, skipping update."
    fi

    # Apply changes to the current session
    # Apply changes to the current session safely (ShellCheck compliant)
    if [ -f "$SHELL_RC" ]; then
    # shellcheck source=/dev/null
    source "$SHELL_RC"
    fi