-
Notifications
You must be signed in to change notification settings - Fork 1
/
floats.go
48 lines (36 loc) · 823 Bytes
/
floats.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
package typenv
import (
"os"
"strconv"
)
// Float64 returns given registered environment variable and mark as used
func Float64(name string, defaults ...float64) float64 {
val, set := os.LookupEnv(name)
if !set {
if 1 == len(defaults) {
return defaults[0]
}
if global, ok := floats[name]; ok {
return global
}
}
f64, err := strconv.ParseFloat(val, 0)
if nil != err {
if 1 == len(defaults) {
return defaults[0]
}
if global, ok := floats[name]; ok {
return global
}
}
return f64
}
// Float32 returns given registered environment variable and mark as used
func Float32(name string, defaults ...float32) float32 {
var subdefaults []float64
if 1 == len(defaults) {
subdefaults = []float64{float64(defaults[0])}
}
f64 := Float64(name, subdefaults...)
return float32(f64)
}