1 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
當(dāng)您不需要區(qū)分空的環(huán)境變量值和未設(shè)置的環(huán)境變量時(shí),使用os.Getenv獲取環(huán)境變量的值。
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.
當(dāng)您確實(shí)需要區(qū)分空的環(huán)境變量值和未設(shè)置的環(huán)境變量時(shí),請(qǐng)使用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獲取所有環(huán)境變量及其值的列表。
func Environ() []string
Environ returns a copy of strings representing the environment, in the form
"key=value".
旁注:也許您沒有正確設(shè)置環(huán)境變量。export SOME_PASS_ENV=some_value在運(yùn)行之前嘗試直接在 shell 中設(shè)置go test
- 1 回答
- 0 關(guān)注
- 171 瀏覽
添加回答
舉報(bào)