Managing Go Versions with goversion

May 26, 2025

🎯 Goal

Switch between multiple versions of Go (Golang) on your system easily using goversion.

🛠 Prerequisites

  • macOS with Homebrew
  • Go is already installed (e.g., Go 1.23 via Homebrew)
  • You use zsh or bash

📦 Step-by-Step Tutorial

1. 🔍 Verify current Go setup

which go
# Example output: /opt/homebrew/opt/[email protected]/bin/go

go env GOPATH
# Example output: /Users/your-username/go

✅ This tells you Go is installed via Homebrew and GOPATH is set to $HOME/go.

2. 🧩 Install goversion

If not already installed:

go install go-simpler.org/goversion@latest

This installs goversion to $GOPATH/bin (usually $HOME/go/bin)

3. 🛤️ Add Go binary path to $PATH

Edit your shell config (~/.zshrc or ~/.bashrc) and add:

export PATH="$HOME/go/bin:$PATH"

Then apply it:

source ~/.zshrc  # or source ~/.bashrc

Verify:

which goversion
# Output: /Users/your-username/go/bin/goversion

4. 📥 Install a specific Go version (e.g. 1.19)

goversion use 1.19

✅ This will:

  • Download and extract Go 1.19 to: /Users/your-username/sdk/go1.19
  • Create a new command: go1.19
  • Set up switching logic (but won't override your default go)

You can now run:

go1.19 version
# Output: go version go1.19 darwin/arm64

5. 🔁 Temporarily switch to Go 1.19

In your current terminal session:

export PATH="$HOME/sdk/go1.19/bin:$PATH"

Check:

go version
# Output: go version go1.19 darwin/arm64

6. 🔒 Permanently set Go 1.19 as default

To always use Go 1.19, add this at the top of your ~/.zshrc or ~/.bashrc:

export PATH="$HOME/sdk/go1.19/bin:$PATH"

Then:

source ~/.zshrc  # or ~/.bashrc

7. 🔄 Switch to another version

You can install and switch to any version:

goversion use 1.22
export PATH="$HOME/sdk/go1.22/bin:$PATH"

Or create aliases like:

alias go19="go1.19"
alias go22="go1.22"

🎯 Managing Multiple Versions

Operation Command Description
Install version goversion use 1.21 Downloads and installs Go 1.21
Switch temporarily export PATH="$HOME/sdk/go1.21/bin:$PATH" Use Go 1.21 in current session
Use specific version go1.21 build Run command with specific Go version
Check version go version Shows currently active Go version

✅ Final Tips

You can now install and manage multiple versions:

goversion use 1.21
goversion use 1.22
goversion use 1.20

Switch by updating PATH:

export PATH="$HOME/sdk/go1.21/bin:$PATH"

Or just use version-specific commands:

go1.19 build
go1.22 test

🚀 Advanced Usage

Create a shell function to easily switch versions:

# Add to ~/.zshrc or ~/.bashrc
switch_go() {
    if [ -z "$1" ]; then
        echo "Usage: switch_go "
        echo "Example: switch_go 1.19"
        return 1
    fi
    
    export PATH="$HOME/sdk/go$1/bin:$PATH"
    echo "Switched to Go $1"
    go version
}

# Usage
switch_go 1.19
switch_go 1.22

Learn More

For more information about goversion, visit the official goversion repository.

#Golang #Go #goversion #macOS #Development #Tutorial #AIGenerated