Go Reference ยท The go Command and Tools
The go env command
Reference for go env: print, set (-w) and unset (-u) Go settings such as GOPATH, GOROOT, GOOS, GOARCH, GOFLAGS, GOPROXY, GOPRIVATE and GOTOOLCHAIN.
go env prints the environment settings the go command uses: where the toolchain lives (GOROOT), where modules and installed tools go (GOPATH, GOBIN), which platform you target (GOOS, GOARCH), how modules are fetched (GOPROXY, GOPRIVATE, GOSUMDB) and many more. It shows the effective value whether it came from the shell, from a stored default, or from the built-in fallback.
It can also change settings for good. go env -w NAME=value stores a value in a per-user configuration file, so every future go command sees it without shell profile edits. This is how people configure proxies, private-module patterns and default flags on a machine.
go env # all settings, as NAME="value" lines go env GOPATH GOOS # only these, one value per line go env -json GOARCH # JSON object go env -w GOPRIVATE=example.com/* go env -u GOPRIVATE # remove the stored value
Try it
The same information is available to a running program through the runtime and go/build packages, and values arrive through ordinary environment variables. The examples read them and imitate the lookup order.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
go env [-json] [-changed] [-u] [-w] [var ...]
| Flag | Description |
|---|---|
| (none) | Print every setting as NAME='value' lines (shell style; set NAME=value on Windows). |
var ... |
Print only the named settings, one value per line, with no name. |
-json |
Print a JSON object instead. |
-changed |
Show only settings whose value differs from the built-in default (Go 1.23). |
-w NAME=value |
Store a default in the user configuration file. |
-u NAME |
Remove a stored default, restoring the built-in behavior. |
Settings you will meet
| Variable | Meaning |
|---|---|
GOROOT |
Root of the Go installation (compiler, standard library). Rarely set by hand. |
GOPATH |
Workspace for the module cache (pkg/mod) and installed tools (bin). Defaults to $HOME/go. |
GOBIN |
Where go install puts executables; defaults to GOPATH/bin. |
GOMODCACHE, GOCACHE |
Locations of the module cache and the build cache. |
GOOS, GOARCH |
Target operating system and architecture for builds. |
CGO_ENABLED |
1 or 0: whether cgo is available. |
GOFLAGS |
Default flags added to every go command (space-separated -flag=value). |
GOPROXY, GOSUMDB, GONOSUMDB |
Module proxy list and checksum database controls. |
GOPRIVATE, GONOPROXY |
Glob patterns of module paths that bypass the proxy and checksum database. |
GOTOOLCHAIN |
Which Go toolchain to use: local, auto, or a version such as go1.24.2+auto. |
GOWORK |
Path to the workspace file, or off. |
GOENV |
Location of the configuration file written by -w. |
GOVERSION, GOMOD |
Read-only: the toolchain version and the path of the current go.mod (/dev/null outside a module). |
Errors: go env -w refuses names it does not know or that cannot be stored, such as the read-only GOVERSION and GOMOD, and it warns when the shell already defines the same name, because the shell wins.
Usage notes
- Order of precedence: a variable in the process environment beats the file written by
go env -w, which beats the default.go envshows the winner. The file lives under your user config directory atgo/env;go env GOENVprints its exact path. - Persistent settings without profiles.
go env -w GOFLAGS=-mod=modorGOPRIVATE=github.com/yourcorp/*is easier than editing.bashrc, and works the same on Windows. - Per-command overrides go before the command:
GOOS=windows GOARCH=amd64 go buildon Unix shells, orset GOOS=windowsfirst on Windowscmd; PowerShell uses$env:GOOS="windows". GOFLAGSaffects everything, includinggo testandgo list, so a leftover-mod=vendorcan surprise you. Usego env GOFLAGSfirst when a command behaves unexpectedly.- Cross-compiling targets.
go tool dist listprints every validGOOS/GOARCHpair; see go build. - Output is machine-friendly. Use
go env -jsonin scripts, not parsing of the default format, and prefer asking for a single variable:$(go env GOPATH)/bin. - Not every setting is listed. Experimental
GOEXPERIMENTand runtimeGODEBUGbehavior are documented separately;GODEBUGcan also be pinned per module ingo.mod. - Inside a program, use
runtime.GOOS,runtime.GOARCHanddebug.ReadBuildInfo()for what the binary was built with; they reflect build time, not the machine it runs on.