-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenv.go
More file actions
49 lines (45 loc) · 787 Bytes
/
env.go
File metadata and controls
49 lines (45 loc) · 787 Bytes
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
package utils
import (
"os"
"strconv"
)
func EnvGet(key string, defaultValue interface{}) interface{} {
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
switch defaultValue.(type) {
case int, int32:
r, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}
return r
case int64:
r, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}
return int64(r)
case bool:
r, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}
return r
case float32:
r, err := strconv.ParseFloat(value, 32)
if err != nil {
return defaultValue
}
return r
case float64:
r, err := strconv.ParseFloat(value, 64)
if err != nil {
return defaultValue
}
return r
default:
return value
}
}