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