Last active
August 18, 2024 15:59
-
-
Save aofei/c55c4c287d56e3ed14cbda3932f9ec81 to your computer and use it in GitHub Desktop.
A shell script for switching Go versions.
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
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
记录一下 需要设置环境变量
export GOBIN=$HOME/bin
export PATH=$GOBIN:$PATH