When working with the Go programming language, inspecting environment settings can help clarify how the toolchain is configured. These values determine paths, architecture, and other important build-related parameters. This tutorial explains how to get Go environment information.
Get all environment variables
The go env command provides a straightforward way to retrieve environment information:
go env
Example output:
GOARCH='amd64'
GOPATH='/home/ubuntu/go'
GOROOT='/usr/local/go'
...
By default, the output is formatted similarly to a shell script, making it easy to reuse in scripts or debugging sessions.
JSON formatted output
To present the environment data in JSON format, use the -json option:
go env -json
Example output:
{
"GOARCH": "amd64",
"GOPATH": "/home/ubuntu/go",
"GOROOT": "/usr/local/go",
...
}
This format is particularly useful for parsing programmatically or integrating with other tools.
Get only modified variables
The -changed option filters the output to display only those variables that differ from their default values:
go env -changed
This helps identify custom configurations that may affect builds or runtime behavior.
Get a specific variable
To retrieve the value of a single environment variable, provide its name as an argument:
go env GOROOT
This prints only the requested value, which is helpful in scripts or quick checks.
Leave a Comment
Cancel reply