Last active
October 29, 2020 13:50
-
-
Save joelanford/a856775bf17b357e175adf131e1f7cdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
function setup-go-env() { | |
CUSTOM_GO_DIR="/usr/local/go" | |
[[ -d "$CUSTOM_GO_DIR/bin" ]] && path+="$CUSTOM_GO_DIR/bin" | |
if command -v go >/dev/null 2>&1; then | |
GOPATH=$(go env GOPATH) | |
path=("$GOPATH/bin" $path) | |
go_major_version=${$(go version | awk '{print $3}' | cut -d'.' -f1)#go} | |
go_minor_version=$(go version | awk '{print $3}' | cut -d'.' -f2) | |
if [[ "$go_major_version" -eq "1" && "$go_minor_version" -lt "13" ]]; then | |
export GOPROXY=https://proxy.golang.org | |
else | |
unset GOPROXY | |
fi | |
fi | |
} | |
setup-go-env | |
# define a function that can be used to quickly download, | |
# install, and use a different Go version | |
function use-go() { | |
if [[ $# != 1 ]]; then | |
echo "ERROR: Must specify which go version to use" | |
return 1 | |
fi | |
local gover=${1#v} | |
local godir="/usr/local/go${gover}" | |
local goos=$(uname | tr '[:upper:]' '[:lower:]') | |
if [[ ! -d "$godir" ]]; then | |
echo "Downloading and installing go${gover}..." | |
sudo mkdir "$godir" | |
if ! $(wget -qO - "https://dl.google.com/go/go${gover}.${goos}-amd64.tar.gz" | sudo tar -C "$godir" --strip-components 1 -z -x -f -); then | |
sudo rmdir "$godir" | |
return 1 | |
fi | |
fi | |
sudo rm -rf "/usr/local/go" | |
sudo ln -s "$godir" "/usr/local/go" | |
setup-go-env | |
echo "Now using Go version: $(go version)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment