LinuxCommandLibrary

go-env

Print Go environment information

TLDR

Show all environment variables

$ go env
copy

Show a specific environment variable
$ go env [GOPATH]
copy

Set an environment variable to a value
$ go env -w [GOBIN]=[path/to/directory]
copy

Reset an environment variable's value
$ go env -u [GOBIN]
copy

SYNOPSIS

go env [-h] [-json] [-u] [-w] [var ...]

PARAMETERS

-h
    Print usage help message

-json
    Output in JSON format instead of shell-export format

-u
    Print names of environment variables modifiable by env vars like GOOS (Go 1.21+)

-w
    Print and write modifiable vars to local go.env file (Go 1.21+)

var
    Optional names of specific vars to display (e.g. GOPATH, GOOS, GOROOT)

DESCRIPTION

The go env command displays environment variables and settings used by the Go build system. By default, it outputs all known variables in a format suitable for shell sourcing, such as export GOPATH=/path. When one or more variable names are specified, only those are printed, one per line.

This tool is essential for developers to inspect the current Go configuration, including paths like GOROOT (Go installation directory), GOPATH (workspace), GOOS and GOARCH (target OS/architecture), and module settings like GOMOD. It aids in troubleshooting builds, CI scripts, and cross-compilation setups.

Flags enable JSON output for programmatic use, or focus on modifiable settings via -u (unsettable vars) and -w (writable vars, updating local go.env files). Requires the Go toolchain installed.

CAVEATS

Output reflects current process env and Go version; may differ across sessions. Requires Go 1.4+. -u and -w need Go 1.21+ and workspace support.

COMMON VARIABLES

GOPATH: Go workspace path.
GOROOT: Go tree installation.
GOOS/GOARCH: Build targets.
GOMOD: Module mode status.

EXAMPLE USAGE

go env GOPATH → prints GOPATH only.
go env -json GOOS GOARCH → JSON output.
go env -w → updates local env.

HISTORY

Introduced in Go 1.4 (2014) for basic env inspection. -json flag added in Go 1.6. -u and -w introduced in Go 1.21 (2023) with go.env file support for local overrides.

SEE ALSO

go(1), go version(1), env(1), printenv(1)

Copied to clipboard