Go Environment Setup on macOS
This page walks through setting up Go on macOS: check or install the toolchain, fix PATH, bootstrap ~/go, install CLI tools with go install, and verify with a minimal module. For a first CLI after setup, see Go for small ops tools.
Check What You Already Have
Section titled “Check What You Already Have”go versionwhich goIf both work, note the version and skip to GOROOT, GOPATH, and modules unless you need a newer release.
Expected output looks like go version go1.22.x darwin/arm64 on Apple Silicon or darwin/amd64 on Intel Macs.
If go is not found, install using one of the options below — pick one path and stick with it.
Avoid mixing Homebrew and the official .pkg installer. Two installs produce confusing which go output and duplicate PATH entries.
Install Go
Section titled “Install Go”Recommended — Homebrew
Section titled “Recommended — Homebrew”Same pattern as other macOS guides in this library:
brew install gogo versionOn Apple Silicon, Homebrew usually puts go on PATH via /opt/homebrew/bin. You typically do not need to add /usr/local/go/bin when you use Homebrew only.
Upgrade later with:
brew upgrade goAlternative — Official .pkg installer
Section titled “Alternative — Official .pkg installer”Download the macOS package from go.dev/dl and run it. This installs Go under /usr/local/go.
Verify:
go versionIf go is not found, add the official binary directory to ~/.zshrc:
export PATH=$PATH:/usr/local/go/binsource ~/.zshrcMultiple Go versions — optional
Section titled “Multiple Go versions — optional”Most developers need one Go release. When you need more:
| Approach | When it fits |
|---|---|
| Official installs from go.dev/dl | Pin a specific version per machine; simple and reliable |
| Homebrew versioned formulae | Team already uses Homebrew for everything |
| asdf with the Go plugin | Same “one tool, many runtimes” idea as pyenv for Python |
| gvm | Legacy option; can be fragile on newer macOS — use only if your team already standardizes on it |
Example gvm flow (optional):
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)gvm install go1.22.0gvm use go1.22.0 --defaultReplace go1.22.0 with a current version from go.dev/dl.
GOROOT, GOPATH, and modules
Section titled “GOROOT, GOPATH, and modules”Go uses a few directory concepts:
GOROOT— where the Go toolchain itself is installed (Homebrew or/usr/local/go).GOPATH— your Go workspace; default~/go. Holdsgo installbinaries (~/go/bin) and the module download cache (~/go/pkg/mod).- Modules — modern projects use
go.modin any folder. You do not need to put source code under$GOPATH/src.
go env GOROOTgo env GOPATHgo env GOMODCACHEgo envInstalled CLI tools from go install land in $GOPATH/bin (usually ~/go/bin).
The ~/go directory does not exist on a fresh install. That is normal — Go creates it when you first go install a tool or run go mod init in a project.
Bootstrap ~/go with goimports
Section titled “Bootstrap ~/go with goimports”Install a standard tool that formats imports and bootstraps your workspace:
go install golang.org/x/tools/cmd/goimports@latestThis will:
- Create
~/go/and~/go/bin/if they are missing - Install the
goimportsbinary under~/go/bin
Verify the binary exists (full path works even before the next step):
ls ~/go/bin# Expected: goimportsgo install does not require $GOPATH/bin on PATH, but which goimports only succeeds after you add that directory to PATH below.
Add Go Tools to Your PATH
Section titled “Add Go Tools to Your PATH”Add these lines to ~/.zshrc so installed tools are callable from anywhere:
export GOPATH=$HOME/goexport PATH=$PATH:$GOPATH/binIf you used the official .pkg installer, also ensure the Go toolchain is on PATH:
export PATH=$PATH:/usr/local/go/binIf you used Homebrew only, the GOPATH and $GOPATH/bin lines above are usually enough — skip /usr/local/go/bin unless which go points there.
Apply changes:
source ~/.zshrcwhich goimportsExample ~/.zshrc blocks
Section titled “Example ~/.zshrc blocks”Homebrew install:
# Go setup (Homebrew)export GOPATH=$HOME/goexport PATH=$PATH:$GOPATH/binOfficial .pkg install:
# Go setup (official installer)export GOPATH=$HOME/goexport PATH=$PATH:/usr/local/go/binexport PATH=$PATH:$GOPATH/binVerify With a Tiny Module
Section titled “Verify With a Tiny Module”Confirm the toolchain end-to-end:
mkdir -p ~/hello-go && cd ~/hello-gogo mod init example.com/helloCreate main.go:
package main
import "fmt"
func main() { fmt.Println("Hello, Go!")}Run it:
go run .Expected: Hello, Go!
For a slightly larger CLI with flags, continue to Go for small ops tools. Cloning private modules may require OpenSSH client keys and GOPRIVATE — see that ops page for a one-line sketch.
Quick Reference
Section titled “Quick Reference”| Task | Command |
|---|---|
| Check Go version | go version |
| Find GOROOT / GOPATH | go env GOROOT / go env GOPATH |
| See all Go env vars | go env |
| Install a Go tool | go install <package>@latest |
| Upgrade (Homebrew) | brew upgrade go |
| Reload shell config | source ~/.zshrc |
Key Takeaways
Section titled “Key Takeaways”- Run
go versionandwhich gobefore installing — avoid duplicate toolchains. - Homebrew vs official
.pkgneed different PATH lines; do not blindly copy both. GOPATH(~/go) holdsgo installtools and the module cache; projects live anywhere withgo mod init.- Bootstrap with
go install …/goimports@latest, then put$GOPATH/binon PATH. - Verify with a tiny module and
go run .before moving on to CLIs or services.
Related Links
Section titled “Related Links”- Go overview — this section’s landing page
- Go for small ops tools — flags, modules, cross-compile
- Go chi service layout — production HTTP service structure
- OpenSSH client keys — clone private Go modules over SSH
- Python project environment setup — parallel macOS setup flow for Python