sgv (Simple Go Version) is a lightweight CLI tool for managing multiple Go versions on your system. It supports Go 1.13 and above, and works on macOS and Linux.
- Install Go versions: Download and install any supported Go version.
- Switch Go versions: Instantly switch between installed Go versions.
- Auto switch: Automatically switch to the required Go version for the current project based on
go.mod. - Get latest: Install and switch to the latest Go version with one command.
- Per-version environment variables: Manage project-specific environment variables for each Go version with automatic loading.
- List installed versions: View all Go versions installed by sgv, grouped by major version.
- List available patch versions: List all available patch versions for a given major version, and see which are installed.
- Uninstall Go versions: Remove any installed Go version (except the currently active one).
- Show sgv version: Display the sgv build version and commit hash.
- Seamless shell integration: Automatic environment variable loading with no manual intervention required.
curl -sSL https://raw.githubusercontent.com/fun7257/sgv/main/install.sh | bash- Installs to
/usr/local/bin/sgv - Automatically configures
GOROOTandPATHin your~/.bashrcor~/.zshrc - After installation, restart your terminal or run
source ~/.bashrcorsource ~/.zshrc
sgv <version>- Example:
sgv 1.22.1orsgv go1.21.0 - If not installed, sgv will download and install the version, then switch
- If in a Go project and the requested version is lower than
go.modrequires, the operation will abort with an error
sgv <version> --no-switch- Example:
sgv 1.22.1 --no-switch - Downloads and installs the version, but does not switch to it.
- If the version is already installed, it will do nothing.
sgv auto- Detects the required Go version from
go.mod(preferstoolchainif present and higher) - If not installed, prompts to download and install
- If already active, does nothing
- If not in a Go project, prints a message and does nothing
sgv latest- Installs the latest Go version if not present, and switches to it
sgv list- Shows all installed versions, grouped by major version
- The current version is marked with
<- current
sgv sub <major_version>- Example:
sgv sub 1.22 - Lists all available Go 1.22.x versions, with installed ones marked
(installed) - Only available for Go 1.13 and above
sgv rm <version...>- Example 1 (specific versions):
sgv rm 1.22.1 1.21.7 - Example 2 (major version):
sgv rm 1.22(removes all installed 1.22.x versions) - Cannot uninstall the currently active version.
sgv version- Shows the Go version used to build sgv and its commit hash
sgv provides sophisticated per-version environment variable management:
sgv env # List environment variables for current Go version
sgv env -w KEY=VALUE # Set environment variable for current Go version
sgv env -u KEY # Remove environment variable for current Go version
sgv env -a # List all Go versions with their environment variables
sgv env --clear # Clear all environment variables for current Go version
sgv env --shell # Output environment variables in shell format
sgv env --shell --clean # Output with cleanup of conflicting variablesExamples:
sgv env -w GOWORK=auto # Enable Go workspace mode
sgv env -w GODEBUG=gctrace=1 # Enable GC trace debugging
sgv env -w CGO_ENABLED=0 # Disable CGO for this Go version
sgv env -u GODEBUG # Remove GODEBUG setting
sgv env -a # See all versions and their variablesKey Features:
- Version isolation: Each Go version has its own environment variables stored separately
- Automatic loading: Environment variables are automatically applied when switching versions
- Protected variables: Critical Go variables (GOROOT, GOPATH, GOPROXY, etc.) cannot be modified through sgv
- Conflict prevention: The
--cleanflag prevents variable conflicts between versions - Persistent storage: Variables are saved in
~/.sgv/env/<version>.envand restored automatically - Shell integration: Changes are immediately applied to your current shell session
sgv provides a seamless experience with automatic environment loading through intelligent shell integration:
- Version switching:
sgv 1.22.1orsgv go1.21.0automatically loads environment variables - Environment changes:
sgv env -w KEY=VALUEandsgv env -u KEYimmediately apply to your current shell - Auto commands:
sgv autoandsgv latestautomatically load environment variables after version switches - Flexible version format: Supports both
1.22.1andgo1.22.1formats
The installation script creates a wrapper function in your shell that:
- Executes the actual
sgvcommand - Detects successful operations that affect environment variables
- Automatically runs
eval $(sgv env --shell --clean)to update your shell - Prevents conflicts by cleaning variables from other versions
- No need to run
evalcommands manually - No need to restart your terminal
- No need to source configuration files
- Environment variables are immediately available after any change
This sophisticated shell integration makes sgv feel like a native part of your development environment.
SGV_DOWNLOAD_URL_PREFIX
Change the Go download source (e.g., for China mainland users)
export SGV_DOWNLOAD_URL_PREFIX=https://golang.google.cn/dl/Set this before running sgv commands, or add to your shell profile for persistence.
sgv organizes files in a predictable way:
~/.sgv/versions/- All installed Go versions (e.g.,~/.sgv/versions/go1.22.1/)~/.sgv/current- Symlink to the currently active Go version~/.sgv/env/- Environment variable files (e.g.,~/.sgv/env/go1.22.1.env)
The installation script automatically adds:
- Environment variable exports (
GOROOT,PATH) - A wrapper function that provides automatic environment loading
- Session startup code to restore environment variables
This configuration is added to ~/.bashrc or ~/.zshrc depending on your shell.
- Cross-platform support: Works on macOS and Linux (Windows is not supported)
- Go version support: Supports Go 1.13 and above
- File organization: All Go versions are installed under
~/.sgv/versions/, with the current version symlinked as~/.sgv/current - Environment isolation: Each Go version maintains its own set of environment variables
- Automatic configuration: The install script handles all
GOROOTandPATHconfiguration automatically - Shell compatibility: Works with both bash and zsh shells
Contributions are welcome! Please open issues or pull requests.
MIT License. See LICENSE for details.