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 [-json] [var ...]
go env -u [var ...]
go env -w [var=value ...]
go env -w -json [var=value ...]

PARAMETERS

-json
    Displays the environment as a JSON object. When combined with -w, outputs JSON suitable for programmatic configuration.

-u
    Unsets the default setting for the specified environment variables in Go's configuration file. This does not affect the current shell's environment.

-w
    Writes the default setting for the specified environment variables to Go's configuration file. These settings will apply to all subsequent Go commands.

DESCRIPTION

The go env command is an integral part of the Go programming language toolchain, used to display and manage Go environment variables.

These variables dictate how Go tools behave, including locations for source code (e.g., GOPATH), compiled binaries (GOBIN), and the target operating system (GOOS) and architecture (GOARCH) for builds. It can list all known Go-specific environment variables or specific ones by name.

Beyond just displaying, go env also provides functionality to persistently set or unset these variables for all subsequent Go commands, making it a crucial utility for configuring a Go development workspace. Understanding go env is fundamental for debugging build issues, managing multiple Go projects, or cross-compiling Go applications.

CAVEATS

The term "go-env" typically refers to the go env subcommand of the main Go toolchain. It is invoked as go env, not as a standalone `go-env` command.

go env operates on Go's internal environment configuration, which may differ from the shell's active environment variables. Changes made with -w are persistent for Go commands but do not alter system-wide or shell-specific environment settings.

ARGUMENTS

When invoked without flags, go env accepts optional arguments in the format to display the value of specific environment variables (e.g., go env GOPATH).

When used with the -w flag, arguments are in the format = to set the default value of the specified variable (e.g., go env -w GOPATH=/home/user/go_projects).

With the -u flag, arguments are simply to unset a specific variable (e.g., go env -u GOPATH).

CONFIGURATION FILE

Settings modified using go env -w are stored in a user-specific configuration file, typically located at $GOENV if set, or $XDG_CONFIG_HOME/go/env on Unix-like systems, or a similar location on Windows. This centralized configuration ensures consistency across different shell sessions and Go commands.

HISTORY

The go env command has been a fundamental part of the Go toolchain since its early releases, evolving with the language's development.

The -json flag was introduced in Go 1.12 to facilitate programmatic interaction with Go environment settings.

The -w flag, allowing persistent setting of environment variables, was added in Go 1.13, simplifying Go workspace configuration without needing to manually modify shell startup scripts.

SEE ALSO

go(1), env(1), export(1)

Copied to clipboard