Skip to content

Instantly share code, notes, and snippets.

@LaurenceJJones
Last active April 23, 2024 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LaurenceJJones/aacedfd4438a811780951b2c40431e3a to your computer and use it in GitHub Desktop.
Save LaurenceJJones/aacedfd4438a811780951b2c40431e3a to your computer and use it in GitHub Desktop.
Script to automatically download golang version
#!/bin/bash
VERSION=${VERSION:-1.22.2}
# Detect OS platform amd64
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
if [ "$ARCH" = "x86_64" ]; then
ARCH="amd64"
fi
# Detect OS platform arm64
if [ "$ARCH" = "aarch64" ]; then
ARCH="arm64"
fi
## Utilities ##
download() {
if [ -z "$1" ]; then
echo "download() requires a URL as first argument"
exit 1
fi
if [ -z "$2" ]; then
echo "download() requires a destination directory as second argument"
exit 1
fi
if [ ! -d "$2" ]; then
echo "$2 is not a directory"
exit 1
fi
if command -v curl >/dev/null; then
cd "$2" || (echo "Could not cd to $2" && exit 1)
# older versions of curl don't support --output-dir
curl -sSLO --fail --remote-name "$1"
cd - >/dev/null || exit
elif command -v wget >/dev/null; then
wget -nv -P "$2" "$1"
else
echo "Neither curl nor wget is available, cannot download files."
exit 1
fi
}
# Download Go
download "https://go.dev/dl/go${VERSION}.${OS}-${ARCH}.tar.gz" "/tmp"
# Exract Go
sudo tar -C /usr/local -xzf "/tmp/go${VERSION}.${OS}-${ARCH}.tar.gz"
# Add Go to PATH
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
# Reload bashrc
echo "Plase reload your bashrc: source ~/.bashrc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment