This document describes how to switch between different Go versions when you have multiple Go versions installed on your system.
Prerequisites
- Multiple Go versions installed (e.g., go1.22, go1.24)
Steps
-
Identify the installation paths of your Go versions.
For example:
- go1.22:
/usr/lib/go-1.22 - go1.24:
/home/xxx/sdk/go1.24.4
- go1.22:
-
Modify your shell configuration file.
The shell configuration file depends on the shell you are using. For example:
- Bash:
~/.bashrcor~/.bash_profile - Zsh:
~/.zshrc - Fish:
~/.config/fish/config.fish
- Bash:
-
Set the
GOROOTandPATHenvironment variables.Add the following lines to your shell configuration file, replacing the paths with the actual installation paths of your desired Go version:
1 2export GOROOT=/path/to/go/version export PATH=$GOROOT/bin:$PATHFor Fish shell, use:
1 2set -gx GOROOT /path/to/go/version set -gx PATH $GOROOT/bin $PATHFor example, to switch to go1.24 in Fish shell, add the following lines to
~/.config/fish/config.fish:1 2set -gx GOROOT /home/xxx/sdk/go1.24.4 set -gx PATH /home/xxx/sdk/go1.24.4/bin $PATH -
Reload your shell configuration.
Run the following command to reload your shell configuration:
1 2 3source ~/.bashrc # For Bash source ~/.zshrc # For Zsh source ~/.config/fish/config.fish # For Fish -
Verify the Go version.
Run the following command to verify that you have successfully switched to the desired Go version:
1go versionThe output should show the Go version you selected.
Example
To switch to go1.24 in Fish shell, follow these steps:
-
Edit
~/.config/fish/config.fishand add the following lines:1 2set -gx GOROOT /home/xxx/sdk/go1.24.4 set -gx PATH /home/xxx/sdk/go1.24.4/bin $PATH -
Run
source ~/.config/fish/config.fishto reload the shell configuration. -
Run
go versionto verify that you are using go1.24.4.