Detailed explanation of Go tool chain (4): Go env, a tool for setting and viewing Golang environment variables

go env function

go env is a command in the Go toolchain, which is used to set and view information about the current Golang environment, and is very useful for understanding, compiling and running Golang programs.

The commands provided by go and the compilation and running of go programs will use environment variables. If the corresponding environment variables are not set, go will use its default settings. By default, env prints environment variables as a shell script (batch file on Windows). If viewing one or more variables specified, the value of each variable occupies one line.

Commonly used parameter descriptions for go env:

  • -json: print environment variables in json format.
  • -u: It needs to be followed by one or more parameter values. If the value of the specified environment variable is set using 'go env -w', the corresponding value setting will be cancelled.
  • -w: Followed by one or more parameters of the form NAME=VALUE and changes the default setting of the specified environment variable to the given value.

How to use go env and usage examples

list go environment variables

go env

The following content will be output. Due to too many contents, only a few are listed, and others are replaced by ellipsis:

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/luduoxin/Library/Caches/go-build"
...

List environment variables in json format:

 go env -json

The following content will be output. Due to too much content, only some parts are listed, and others are replaced by ellipsis:

{
        "AR": "ar",
        "CC": "clang",
        "CGO_CFLAGS": "-g -O2",
				...
}

Check out one or several go environment variables:

$ go env GOPATH 
/Users/luduoxin/go
$ go env GOPATH GOROOT
/Users/luduoxin/go
/usr/local/opt/go/libexec

Change one or several go environment variables:

$ go env GOPATH=/Users/ning/go
$ go env GOPATH=/Users/ning/go GOROOT=/usr/local/go/libexec

If the corresponding environment variable has been configured in the operating system environment variable configuration file, it is invalid to use go env -w to modify it, and the following error will be reported:

warning: go env -w GOPATH=... does not override conflicting OS environment variable

Go environment variables and their functions

Next, look at the general environment variables:

  • GO111MODULE: used to control whether to use go mod mode or GOPATH mode, the value is off, on or auto.
  • GCCGO: The gccgo command runs 'go build -compiler=gccgo'.
  • GOARCH: current architecture, such as amd64, 386, arm, ppc64, etc.
  • GOBIN: The default binary directory for the Go command, which is the directory where 'go install' will install the command.
  • GOCACHE: Cache directory for Go commands.
  • GOMODCACHE: The directory where the go command will store downloaded modules.
  • GODEBUG: enable various debugging tools, see 'go doc runtime'.
  • GOENV: The location of the Go environment variable configuration file.
  • GOFLAGS: The flag parameter of the Go command.
  • GOINSECURE: Used to specify which domain name warehouses do not verify CA certificates.
  • GOOS: current operating system, such as linux, darwin, windows, netbsd, etc.
  • GOPATH: The root directory of the Go project.
  • GOPROXY: The URL of the Go module proxy.
  • GOROOT: The root directory of Golang installation.
  • GOSUMDB: The name of the validation database to use and optionally the public key and URL
  • GOTMPDIR: The directory where the go command writes temporary source files, packages and binaries.
  • GOVCS: List version control commands that may be used with the match server.
  • GOWORK: workspace settings.

In addition to general environment variables, there are environment variables related to the use of cgo, environment variables of specific system architecture, special environment variables and environment variables that can only be read through go env. help environment command to view.

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131862003