-
Notifications
You must be signed in to change notification settings - Fork 3
/
int_slice.go
62 lines (47 loc) · 1 KB
/
int_slice.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
58
59
60
61
62
package env
import (
"fmt"
"os"
"strconv"
"strings"
)
// GetIntSlice extracts slice of int value with the format "1,2,3" from env. if not set, returns default value.
func GetIntSlice(key string, def []int) []int {
s, ok := os.LookupEnv(key)
if !ok {
return def
}
if s == "" {
return []int{}
}
ss := strings.Split(s, ",")
res := make([]int, len(ss))
for i := range ss {
v, err := strconv.Atoi(ss[i])
if err != nil {
return def
}
res[i] = v
}
return res
}
// MustGetIntSlice extracts slice of int value with the format "1,2,3" from env. if not set, it panics.
func MustGetIntSlice(key string) []int {
s, ok := os.LookupEnv(key)
if !ok {
panic(fmt.Sprintf("environment variable '%s' not set", key))
}
if s == "" {
return []int{}
}
ss := strings.Split(s, ",")
res := make([]int, len(ss))
for i := range ss {
v, err := strconv.Atoi(ss[i])
if err != nil {
panic(fmt.Sprintf("invalid environment variable '%s' has been set: %s", key, s))
}
res[i] = v
}
return res
}