mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 17:16:38 +08:00
44 lines
803 B
Go
44 lines
803 B
Go
package env
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func Bool(env string, defaultValue bool) bool {
|
|
if env == "" || os.Getenv(env) == "" {
|
|
return defaultValue
|
|
}
|
|
return strings.ToLower(os.Getenv(env)) == "true"
|
|
}
|
|
|
|
func Int(env string, defaultValue int) int {
|
|
if env == "" || os.Getenv(env) == "" {
|
|
return defaultValue
|
|
}
|
|
num, err := strconv.Atoi(os.Getenv(env))
|
|
if err != nil {
|
|
return defaultValue
|
|
}
|
|
return num
|
|
}
|
|
|
|
func Float64(env string, defaultValue float64) float64 {
|
|
if env == "" || os.Getenv(env) == "" {
|
|
return defaultValue
|
|
}
|
|
num, err := strconv.ParseFloat(os.Getenv(env), 64)
|
|
if err != nil {
|
|
return defaultValue
|
|
}
|
|
return num
|
|
}
|
|
|
|
func String(env string, defaultValue string) string {
|
|
if env == "" || os.Getenv(env) == "" {
|
|
return defaultValue
|
|
}
|
|
return os.Getenv(env)
|
|
}
|