-
Notifications
You must be signed in to change notification settings - Fork 3
/
float32_slice.go
63 lines (48 loc) · 1.12 KB
/
float32_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
63
package env
import (
"fmt"
"os"
"strconv"
"strings"
)
// GetFloat32Slice extracts slice of float32 value with the format "1.2,2.3,3.4" from env.
// if not set, returns default value.
func GetFloat32Slice(key string, def []float32) []float32 {
s, ok := os.LookupEnv(key)
if !ok {
return def
}
if s == "" {
return []float32{}
}
ss := strings.Split(s, ",")
res := make([]float32, len(ss))
for i := range ss {
v, err := strconv.ParseFloat(ss[i], bitSize32)
if err != nil {
return def
}
res[i] = float32(v)
}
return res
}
// MustGetFloat32Slice extracts slice of float32 value with the format "1.2,2.3,3.4" from env. if not set, it panics.
func MustGetFloat32Slice(key string) []float32 {
s, ok := os.LookupEnv(key)
if !ok {
panic(fmt.Sprintf("environment variable '%s' not set", key))
}
if s == "" {
return []float32{}
}
ss := strings.Split(s, ",")
res := make([]float32, len(ss))
for i := range ss {
v, err := strconv.ParseFloat(ss[i], bitSize32)
if err != nil {
panic(fmt.Sprintf("invalid environment variable '%s' has been set: %s", key, s))
}
res[i] = float32(v)
}
return res
}