Skip to content

Instantly share code, notes, and snippets.

@aofei
Last active August 18, 2024 15:59
Show Gist options
  • Save aofei/c55c4c287d56e3ed14cbda3932f9ec81 to your computer and use it in GitHub Desktop.
Save aofei/c55c4c287d56e3ed14cbda3932f9ec81 to your computer and use it in GitHub Desktop.
A shell script for switching Go versions.
#!/bin/sh
set -e
if [[ "$#" -ne 1 ]]; then
echo "Usage: switchgo version" >&2
exit 2
fi
if [[ -z "${GOBIN}" ]]; then
echo "GOBIN is not set" >&2
exit 2
fi
if ! command -v "${GOBIN}/go${1}" &> /dev/null; then
echo "Go $1 doesn't exist, start downloading..."
if [[ "$(go version | cut -d " " -f 3 | cut -d . -f 2)" -ge 17 ]]; then
go install "golang.org/dl/go${1}@latest"
else
go get "golang.org/dl/go${1}"
fi
"${GOBIN}/go${1}" download
fi
ln -sf "$(command -v "${GOBIN}/go${1}")" "${GOBIN}/go"
echo "Switched to Go $1"
@cobb-tx
Copy link

cobb-tx commented Jul 22, 2020

记录一下 需要设置环境变量
export GOBIN=$HOME/bin
export PATH=$GOBIN:$PATH

@cobb-tx
Copy link

cobb-tx commented Jul 22, 2020

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment