Skip to content

Instantly share code, notes, and snippets.

@carterjones
Created April 25, 2018 15:34
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 carterjones/8d40fa8040e64bc4e254fe9f4e38e4eb to your computer and use it in GitHub Desktop.
Save carterjones/8d40fa8040e64bc4e254fe9f4e38e4eb to your computer and use it in GitHub Desktop.
install golang on linux or mac
#!/bin/bash
set -euxo pipefail
target_version="go1.10.1"
# Test to see if go is installed in the PATH.
if which go &>/dev/null; then
# See which version is installed.
installed_version=$(go version | cut -d" " -f3)
if [[ "$target_version" == "$installed_version" ]]; then
# Exit if the target version matches the installed version.
echo "Version $target_version of go is already installed."
exit
fi
fi
if [[ $(uname) == Linux ]]; then
archive_name="${target_version}.linux-amd64.tar.gz"
elif [[ $(uname) == Darwin ]]; then
archive_name="${target_version}.darwin-amd64.pkg"
elif [[ $(uname) == MINGW* ]]; then
echo "Windows installations are not supported by this installer."
exit
fi
golang_url="https://storage.googleapis.com/golang/${archive_name}"
pushd /tmp
wget -nc -q $golang_url
if [[ $(uname) == Linux ]]; then
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf $archive_name
if which update-alternatives; then
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 0
sudo update-alternatives --set go /usr/local/go/bin/go
fi
elif [[ $(uname) == Darwin ]]; then
sudo installer -pkg $archive_name -target /
fi
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment