GO語言"os"包中"LookupEnv"函數的用法及代碼示例。
用法:
func LookupEnv(key string)(string, bool)
LookupEnv 檢索由鍵命名的環境變量的值。如果變量存在於環境中,則返回值(可能為空)並且布爾值為真。否則返回值為空,布爾值為假。
例子:
package main
import (
"fmt"
"os"
)
func main() {
show := func(key string) {
val, ok := os.LookupEnv(key)
if !ok {
fmt.Printf("%s not set\n", key)
} else {
fmt.Printf("%s=%s\n", key, val)
}
}
os.Setenv("SOME_KEY", "value")
os.Setenv("EMPTY_KEY", "")
show("SOME_KEY")
show("EMPTY_KEY")
show("MISSING_KEY")
}
輸出:
SOME_KEY=value EMPTY_KEY= MISSING_KEY not set
相關用法
- GO LookPath用法及代碼示例
- GO Logger.Output用法及代碼示例
- GO Log10用法及代碼示例
- GO Log用法及代碼示例
- GO Logger用法及代碼示例
- GO LoadLocation用法及代碼示例
- GO LoadX509KeyPair用法及代碼示例
- GO Location用法及代碼示例
- GO Log2用法及代碼示例
- GO LeadingZeros32用法及代碼示例
- GO LastIndex用法及代碼示例
- GO LeadingZeros8用法及代碼示例
- GO LeadingZeros16用法及代碼示例
- GO ListenAndServe用法及代碼示例
- GO Listener用法及代碼示例
- GO LastIndexAny用法及代碼示例
- GO Len8用法及代碼示例
- GO ListenAndServeTLS用法及代碼示例
- GO LastIndexFunc用法及代碼示例
- GO Len64用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 LookupEnv。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。