-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
57 lines (51 loc) · 960 Bytes
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package util
import (
"os"
"strconv"
"time"
)
func GetEnvStr(key string, def string) string {
if env, ok := os.LookupEnv(key); ok {
return env
}
return def
}
func GetEnvInt64(key string, def int64) int64 {
str := GetEnvStr(key, "")
if str == "" {
return def
}
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return def
}
return i
}
func GetEnvUint64(key string, def uint64) uint64 {
str := GetEnvStr(key, "")
if str == "" {
return def
}
i, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return def
}
return i
}
func GetEnvInt(key string, def int) int {
return int(GetEnvInt64(key, int64(def)))
}
func GetEnvUint(key string, def uint) uint {
return uint(GetEnvUint64(key, uint64(def)))
}
func GetEnvDuration(key string, def time.Duration) time.Duration {
str := GetEnvStr(key, "")
if str == "" {
return def
}
duration, err := time.ParseDuration(str)
if err != nil {
return def
}
return duration
}