帮助文档:
$ go help envusage: 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 variablenames is given as arguments, env prints the value ofeach named variable on its own line.The -json flag prints the environment in JSON formatinstead of as a shell script.The -u flag requires one or more arguments and unsetsthe 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 theform NAME=VALUE and changes the default settingsof 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,directgo env -w GO111MODULE=on
