Skip to content

Instantly share code, notes, and snippets.

@navarrothiago
Created February 25, 2023 14:40
Show Gist options
  • Save navarrothiago/6ac63ed05320a105d4f0010291a1e870 to your computer and use it in GitHub Desktop.
Save navarrothiago/6ac63ed05320a105d4f0010291a1e870 to your computer and use it in GitHub Desktop.
Installs Go locally in the current directory
#!/usr/bin/env bash
# Installs Go locally, in the current directory
# Usage:
# ./go-install-locally.sh [GO_VERSION]
# Example:
# ./install-go.sh 1.16
# The script will install Go 1.16 locally in the current directory.
# The script will only install Go if it is not already installed.
# The script will do nothing if Go is already installed.
main() {
set -o errexit
set -o pipefail
set -o nounset
local -r __dirname="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local -r __filename="${__dirname}/$(basename "${BASH_SOURCE[0]}")"
GO_VERSION=${1:-1.16}
echo "Checking if Go $GO_VERSION is already installed locally @ $__dirname/go"
if [[ -f $__dirname/go/.installed-$GO_VERSION ]]; then
echo "Go $GO_VERSION is already installed locally @ $__dirname/go"
exit 0
fi
echo "Removing any existing Go installation @ $__dirname/go"
rm -Rf go
echo "Installing Go $GO_VERSION locally @ $__dirname/go"
wget -cq https://go.dev/dl/go$GO_VERSION.linux-amd64.tar.gz -O - | tar xz
touch $__dirname/go/.installed-$GO_VERSION
echo "Go $GO_VERSION is now installed"
exit 0
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment