Skip to content

Instantly share code, notes, and snippets.

@csknk
Created July 4, 2022 15:00
Show Gist options
  • Save csknk/72ccb2f651d56e5bad82b48e71263984 to your computer and use it in GitHub Desktop.
Save csknk/72ccb2f651d56e5bad82b48e71263984 to your computer and use it in GitHub Desktop.
Switch Go Versions
#!/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}"
@csknk
Copy link
Author

csknk commented Jul 4, 2022

Requires go versions to be added under /usr/lib, e.g:

/usr/lib                                                                                                
├── go -> /usr/lib/go-1.18                                                                                             
├── go-1.17                                                                                                            
├── go-1.18

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