gateway/utils/env_utils.go

42 lines
732 B
Go
Raw Normal View History

2021-09-07 14:25:39 +08:00
package utils
import (
"os"
"strconv"
)
// 从环境变量获取字符串类型值
func GetEnvVal(key, defaultVal string) string {
val, exist := os.LookupEnv(key)
if !exist {
return defaultVal
}
return val
}
// 从环境变量获取数字类型值
func GetEnvIntVal(key string, defaultVal int) int {
valStr, exist := os.LookupEnv(key)
if !exist {
return defaultVal
}
val, err := strconv.Atoi(valStr)
if err != nil {
return defaultVal
}
return val
}
// 从环境变量获取数字类型值
func GetEnvBoolVal(key string, defaultVal bool) bool {
valStr, exist := os.LookupEnv(key)
if !exist {
return defaultVal
}
val, err := strconv.ParseBool(valStr)
if err != nil {
return defaultVal
}
return val
}