Skip to content

Instantly share code, notes, and snippets.

@abhishekkr
Last active November 12, 2019 14:13
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save abhishekkr/80abac819c55458a66e32a49f4e7d608 to your computer and use it in GitHub Desktop.
easy version manager for golang, default at `~/bin` for `v1.13.4`
#!/usr/bin/env bash
### usage example:$ go-versions 1.13.4
set -e
export GOVERSION="$1"
[[ ! -z "$2" ]] && export LOCAL_BIN="$2"
[[ -z "${LOCAL_BIN}" ]] && export LOCAL_BIN="${HOME}/bin"
[[ ! -d "${LOCAL_BIN}" ]] && mkdir -p "${LOCAL_BIN}"
[[ -z "${GOVERSION}" ]] && export GOVERSION="1.13.4"
[[ -z "${GOOS}" ]] && export GOOS="linux"
[[ -z "${GOARCH}" ]] && export GOARCH="amd64"
export GOROOT="${LOCAL_BIN}/go-${GOVERSION}"
export GO_DIST_URI="https://storage.googleapis.com/golang/go${GOVERSION}.${GOOS}-${GOARCH}.tar.gz"
export FALLBACK_GO_DIST_URI="https://dl.google.com/go/go${GOVERSION}.${GOOS}-${GOARCH}.tar.gz"
export GO_DIST_LOCAL="/tmp/go-${GOVERSION}.tar.gz"
if [[ ! -f "${GOROOT}/bin/go" ]]; then
set -x
wget -c -O "${GO_DIST_LOCAL}" "${GO_DIST_URI}"
if [[ $? -ne 0 ]]; then
echo "failed to download ${GO_DIST_URI}, trying fallback"
wget -c -O "${GO_DIST_LOCAL}" "${FALLBACK_GO_DIST_URI}"
if [[ $? -ne 0 ]]; then
echo "failed to download ${FALLBACK_GO_DIST_URI}"
exit 123
fi
fi
tar zxf "${GO_DIST_LOCAL}" -C "${LOCAL_BIN}"
mv "${LOCAL_BIN}/go" "${GOROOT}"
set +x
else
echo "the required Go binary is locally available"
fi
export PATH="${GOROOT}/bin:${PATH}"
echo ""
echo "add following line to your profile:"
echo ""
echo " export PATH=\"${GOROOT}/bin:\${PATH}\""
echo ""
echo "or below as advanced helpere, which also provides 'go-defaults' foo to switch version"
echo ""
echo "go-defaults(){"
echo " local _VERSION=\"\${1:-\${GOVERSION_DEFAULT}}\""
echo " export GOVERSION=\"\${_VERSION}\""
echo " export GOROOT=\"\$HOME/bin/go-\${GOVERSION}\""
echo " export PATH=\"\${GOROOT}/bin:\${PATH}\""
echo "}"
echo "export GOVERSION_DEFAULT=\"1.13.4\""
echo "go-defaults"
######################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment