-
-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathenv.go
More file actions
27 lines (22 loc) · 717 Bytes
/
env.go
File metadata and controls
27 lines (22 loc) · 717 Bytes
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
package torrent
import (
"os"
"strconv"
"github.com/anacrolix/missinggo/v2/panicif"
"golang.org/x/exp/constraints"
)
func initIntFromEnv[T constraints.Signed](key string, defaultValue T, bitSize int) T {
return strconvFromEnv(key, defaultValue, bitSize, strconv.ParseInt)
}
func initUIntFromEnv[T constraints.Unsigned](key string, defaultValue T, bitSize int) T {
return strconvFromEnv(key, defaultValue, bitSize, strconv.ParseUint)
}
func strconvFromEnv[T, U constraints.Integer](key string, defaultValue T, bitSize int, conv func(s string, base, bitSize int) (U, error)) T {
s := os.Getenv(key)
if s == "" {
return defaultValue
}
i64, err := conv(s, 10, bitSize)
panicif.Err(err)
return T(i64)
}