Skip to content

Instantly share code, notes, and snippets.

@navossoc
Last active August 23, 2025 21:22
Show Gist options
  • Select an option

  • Save navossoc/f77070014de9e899e271440e0cd9f8ec to your computer and use it in GitHub Desktop.

Select an option

Save navossoc/f77070014de9e899e271440e0cd9f8ec to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eu
LATEST=$(curl -fsL https://golang.org/VERSION?m=text)
LATEST=$(echo $LATEST | sed -r 's/go([0-9.]+) time.*\b/\1/')
echo "Latest version: $LATEST"
echo
VERSION=${VERSION:-$LATEST}
OS=${OS:-linux}
ARCH=${ARCH:-amd64}
echo Version: $VERSION
echo OS: $OS
echo Arch: $ARCH
echo
FILENAME=$VERSION.$OS-$ARCH
echo Downloading $FILENAME...
echo
curl https://dl.google.com/go/go$FILENAME.tar.gz -# -o /tmp/go.tar.gz
echo Uninstall old version...
echo
sudo rm -rf /usr/local/go/
echo Installing new version...
echo
sudo tar -C /usr/local -xzf /tmp/go.tar.gz
rm -rf /tmp/go.tar.gz
# Add Go to PATH if not already present
GO_PATH="/usr/local/go/bin"
if ! echo "$PATH" | grep -q "$GO_PATH"; then
echo "Adding Go to PATH..."
# Determine which shell profile to update
if [ -n "${BASH_VERSION:-}" ] && [ -f "$HOME/.bashrc" ]; then
PROFILE="$HOME/.bashrc"
elif [ -n "${ZSH_VERSION:-}" ] && [ -f "$HOME/.zshrc" ]; then
PROFILE="$HOME/.zshrc"
elif [ -f "$HOME/.bash_profile" ]; then
PROFILE="$HOME/.bash_profile"
elif [ -f "$HOME/.profile" ]; then
PROFILE="$HOME/.profile"
else
PROFILE="$HOME/.bashrc"
fi
# Check if the PATH export is already in the profile
if ! grep -q "export PATH.*$GO_PATH" "$PROFILE" 2>/dev/null; then
echo "export PATH=\$PATH:$GO_PATH" >> "$PROFILE"
echo "Added Go to PATH in $PROFILE"
else
echo "Go PATH already exists in $PROFILE"
fi
# Export for current session
export PATH=$PATH:$GO_PATH
fi
go version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment