Last active
January 7, 2025 05:16
-
-
Save aofei/c55c4c287d56e3ed14cbda3932f9ec81 to your computer and use it in GitHub Desktop.
A shell script for switching Go versions.
This file contains hidden or 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" |
Comments are disabled for this gist.