Created
July 4, 2022 15:00
-
-
Save csknk/72ccb2f651d56e5bad82b48e71263984 to your computer and use it in GitHub Desktop.
Switch 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
#!/usr/bin/env bash | |
set -eo pipefail | |
IFS=$'\n\t' | |
GREEN='\033[32;1m' | |
RED='\033[91;1m' | |
RESET='\033[0m' | |
function colecho { | |
echo -e "${2}${1}${RESET}" | |
} | |
function usage() { | |
cat <<- EOF | |
Switch between go version 1.17 and 1.18 | |
$0 <1.17 | 1.18> | |
EOF | |
} | |
[[ $(whoami) != root ]] && { colecho "Run as root user or using sudo." "${RED}"; usage; exit 1; } | |
[[ -z $1 ]] && { colecho "Parameter required: 1.17 or 1.18." "${RED}"; usage; exit 2; } | |
set -u | |
case "$1" in | |
"1.17" ) | |
colecho "1.17" "${GREEN}" | |
ln -sf /usr/lib/go-1.17/bin/go /usr/bin/go | |
ln -sf /usr/lib/go-1.17/bin/gofmt /usr/bin/gofmt | |
;; | |
"1.18" ) | |
colecho "1.18" "${GREEN}" | |
ln -sf /usr/lib/go-1.18/bin/go /usr/bin/go | |
ln -sf /usr/lib/go-1.18/bin/gofmt /usr/bin/gofmt | |
;; | |
* ) | |
colecho "Unrecognized option" "${RED}" | |
usage | |
;; | |
esac | |
colecho "Version: $(go version)" "${GREEN}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires go versions to be added under
/usr/lib
, e.g: