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