帮助文档:
$ go help env
usage: go env [-json] [-u] [-w] [var ...]
Env prints Go environment information.
By default env prints information as a shell script
(on Windows, a batch file). If one or more variable
names is given as arguments, env prints the value of
each named variable on its own line.
The -json flag prints the environment in JSON format
instead of as a shell script.
The -u flag requires one or more arguments and unsets
the default setting for the named environment variables,
if one has been set with 'go env -w'.
The -w flag requires one or more arguments of the
form NAME=VALUE and changes the default settings
of the named environment variables to the given values.
For more about environment variables, see 'go help environment'.
查看更详细帮助文档:
$ go help environment
查看当前环境变量:
$ go env --json
获取某个环境变量的值:
$ go env <NAME>
改变环境变量的值:
$ go env -w <NAME>=<VALUE>
上述命令会更改默认设置,并记录在 Go 的环境配置文件,该文件存储在用户的配置目录中,可通过 os.UserConfigDir
函数获取。Windows 系统下路径可能为:
C:\Users\<user_name>\AppData\Roaming\go\env
通过 go env -w
设置的值不会覆盖系统设置的相关环境变量。
常用的设置有:
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GO111MODULE=on