-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
38 lines (34 loc) · 865 Bytes
/
float.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
package env
import (
"fmt"
"os"
"github.com/spf13/cast"
)
// Float64 returns the float64 value of the environment variable named by the key.
func Float64(key string, fallback ...float64) float64 {
if value, ok := os.LookupEnv(key); ok {
if value, err := cast.ToFloat64E(value); err != nil {
panic(fmt.Errorf("%s: %w", key, err))
} else {
return value
}
}
if len(fallback) > 0 {
return fallback[0]
}
panic(fmt.Errorf("%s: missing", key))
}
// Float32 returns the float32 value of the environment variable named by the key.
func Float32(key string, fallback ...float32) float32 {
if value, ok := os.LookupEnv(key); ok {
if value, err := cast.ToFloat32E(value); err != nil {
panic(fmt.Errorf("%s: %w", key, err))
} else {
return value
}
}
if len(fallback) > 0 {
return fallback[0]
}
panic(fmt.Errorf("%s: missing", key))
}