kos/util/env/env.go

42 lines
649 B
Go
Raw Normal View History

2023-04-23 17:57:36 +08:00
package env
import (
"os"
"strconv"
"strings"
)
func Get(name string, val string) string {
value := strings.TrimSpace(os.Getenv(name))
if value == "" {
return val
} else {
return value
}
}
func Integer(name string, val int64) int64 {
value := Get(name, "")
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
return n
} else {
return val
}
}
func Float(name string, val float64) float64 {
value := Get(name, "")
if n, err := strconv.ParseFloat(value, 64); err == nil {
return n
} else {
return val
}
}
func Set(name string, val string) {
value := os.Getenv(name)
if value == "" {
os.Setenv(name, val)
}
}