1 回答
TA贡献1864条经验 获得超2个赞
当您不需要区分空的环境变量值和未设置的环境变量时,使用os.Getenv获取环境变量的值。
func Getenv(key string) string
Getenv retrieves the value of the environment variable named by the key. It
returns the value, which will be empty if the variable is not present. To
distinguish between an empty value and an unset value, use LookupEnv.
当您确实需要区分空的环境变量值和未设置的环境变量时,请使用os.Lookupenv 。
func LookupEnv(key string) (string, bool)
LookupEnv retrieves the value of the environment variable named by the key.
If the variable is present in the environment the value (which may be empty)
is returned and the boolean is true. Otherwise the returned value will be
empty and the boolean will be false.
使用os.Environ获取所有环境变量及其值的列表。
func Environ() []string
Environ returns a copy of strings representing the environment, in the form
"key=value".
旁注:也许您没有正确设置环境变量。export SOME_PASS_ENV=some_value在运行之前尝试直接在 shell 中设置go test
- 1 回答
- 0 关注
- 135 浏览
添加回答
举报