Created
October 2, 2025 04:03
-
-
Save ySnoopyDogy/1ffcbac05ce281d2077340489285b990 to your computer and use it in GitHub Desktop.
Bash script to change the current user Go's version to any installed
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
| #!/usr/bin/env bash | |
| # Just let the versions ziped files in the same directory as the script | |
| # For reference, in my ~/document/multiversiongolang I have: | |
| # 1.18.gz 1.21.gz 1.22.gz 1.23.gz 1.25.gz change_go_version.sh | |
| # In .bashrc, I got the alias: | |
| # alias cgo='bash ~/documents/multiversiongolang/change_go_version.sh' | |
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| for f in "$script_dir"/*.gz; do | |
| [[ -f $f ]] && valid_versions+=("$(basename "${f%.gz}")") | |
| done | |
| if [ -z $1 ]; then | |
| echo "No version selected. Chose between ${valid_versions[@]}" | |
| exit 1 | |
| fi | |
| input_version="$1" | |
| for version in "${valid_versions[@]}"; do | |
| if [[ "$input_version" == "$version" ]]; then | |
| sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf "$script_dir/$version.gz" | |
| go version | |
| exit 0 | |
| fi | |
| done | |
| echo "Error: Version $input_version is not valid. Choose between ${valid_versions[@]}" >&2 | |
| exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment