@@ -2,7 +2,6 @@ package env
2
2
3
3
import (
4
4
"bytes"
5
- "math"
6
5
"os"
7
6
"regexp"
8
7
"runtime"
@@ -54,18 +53,29 @@ var (
54
53
// Initializer.
55
54
func init () {
56
55
// Set the number of parallel parsing tasks.
57
- Together (runtime .NumCPU ())
56
+ ParallelTasks (runtime .NumCPU ())
58
57
}
59
58
60
59
// Together sets the number of parallel transliteration tasks.
61
- func Together (pt int ) int {
60
+ func ParallelTasks (pt int ) int {
62
61
// The maximum number of parallel tasks
63
62
// is twice the number of CPU cores.
64
63
max := runtime .NumCPU () * 2
65
64
65
+ // clamp returns the value limited to the given range [min, max].
66
+ clamp := func (value , min , max int ) int {
67
+ if value < min {
68
+ return min
69
+ }
70
+ if value > max {
71
+ return max
72
+ }
73
+ return value
74
+ }
75
+
66
76
// The minimum number of parallel tasks is 2.
67
77
// And the maximum number is twice the number of CPU cores.
68
- parallelTasks = int ( math . Min ( float64 ( max ), math . Max ( float64 ( pt ) , 2 )) )
78
+ parallelTasks = clamp ( pt , 2 , max )
69
79
return parallelTasks
70
80
}
71
81
@@ -76,7 +86,7 @@ func Together(pt int) int {
76
86
// Returns an error if the env-file contains incorrect data,
77
87
// file is damaged or missing.
78
88
//
79
- // # Examples
89
+ // Examples:
80
90
//
81
91
// In this example, some variables are already set in the environment:
82
92
//
@@ -318,26 +328,26 @@ func Save(filename, prefix string, obj interface{}) error {
318
328
//
319
329
// Check if a variable exists in the environment:
320
330
//
321
- // fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
322
- // fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
323
- // fmt.Printf("KEY_0 and KEY_1 is %v\n\n", env.Exists("KEY_0", "KEY_1"))
324
- //
325
- // if err := env.Update("./cmd/.env"); err != nil {
326
- // log.Fatal(err)
327
- // }
328
- //
329
- // fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
330
- // fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
331
- // fmt.Printf("KEY_0 and KEY_1 is %v\n", env.Exists("KEY_0", "KEY_1"))
332
- //
333
- // // Output:
334
- // // KEY_0 is true
335
- // // KEY_1 is false
336
- // // KEY_0 and KEY_1 is false
337
- // //
338
- // // KEY_0 is true
339
- // // KEY_1 is true
340
- // // KEY_0 and KEY_1 is true
331
+ // fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
332
+ // fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
333
+ // fmt.Printf("KEY_0 and KEY_1 is %v\n\n", env.Exists("KEY_0", "KEY_1"))
334
+ //
335
+ // if err := env.Update("./cmd/.env"); err != nil {
336
+ // log.Fatal(err)
337
+ // }
338
+ //
339
+ // fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
340
+ // fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
341
+ // fmt.Printf("KEY_0 and KEY_1 is %v\n", env.Exists("KEY_0", "KEY_1"))
342
+ //
343
+ // // Output:
344
+ // // KEY_0 is true
345
+ // // KEY_1 is false
346
+ // // KEY_0 and KEY_1 is false
347
+ // //
348
+ // // KEY_0 is true
349
+ // // KEY_1 is true
350
+ // // KEY_0 and KEY_1 is true
341
351
func Exists (keys ... string ) bool {
342
352
for _ , key := range keys {
343
353
if _ , ok := os .LookupEnv (key ); ! ok {
@@ -443,10 +453,10 @@ func Unmarshal(prefix string, obj interface{}) error {
443
453
// Use the following tags in the fields of structure to
444
454
// set the marshing parameters:
445
455
//
446
- // env matches the name of the key in the environment;
447
- // def default value (if empty, sets the default value
448
- // for the field type of structure);
449
- // sep sets the separator for lists/arrays (default ` ` - space).
456
+ // - env matches the name of the key in the environment;
457
+ // - def default value (if empty, sets the default value
458
+ // for the field type of structure);
459
+ // - sep sets the separator for lists/arrays (default ` ` - space).
450
460
//
451
461
// Structure example:
452
462
//
0 commit comments