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