-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcfg_examples_test.go
78 lines (63 loc) · 1.49 KB
/
cfg_examples_test.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cfg_test
import (
"fmt"
"log"
"time"
"github.com/ardanlabs/kit/cfg"
)
// ExampleGlobal shows how to use the package level funcs of the config
// package.
func ExampleGlobal() {
// Init() must be called only once with the given namespace to load.
cfg.Init(cfg.MapProvider{
Map: map[string]string{
"IP": "40.23.233.10",
"PORT": "4044",
"INIT_STAMP": time.Date(2009, time.November,
10, 15, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate),
"FLAG": "on",
},
})
// To get the ip.
fmt.Println(cfg.MustString("IP"))
// To get the port number.
fmt.Println(cfg.MustInt("PORT"))
// To get the timestamp.
fmt.Println(cfg.MustTime("INIT_STAMP"))
// To get the flag.
fmt.Println(cfg.MustBool("FLAG"))
// Output:
// 40.23.233.10
// 4044
// 2009-11-10 15:00:00 +0000 UTC
// true
}
// ExampleNew shows how to create and use a new config which can be passed
// around.
func ExampleNew() {
c, err := cfg.New(cfg.MapProvider{
Map: map[string]string{
"IP": "80.23.233.10",
"PORT": "8044",
"INIT_STAMP": time.Date(2009, time.November,
10, 23, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate),
"FLAG": "off",
},
})
if err != nil {
log.Fatal(err)
}
// To get the ip.
fmt.Println(c.MustString("IP"))
// To get the port number.
fmt.Println(c.MustInt("PORT"))
// To get the timestamp.
fmt.Println(c.MustTime("INIT_STAMP"))
// To get the flag.
fmt.Println(c.MustBool("FLAG"))
// Output:
// 80.23.233.10
// 8044
// 2009-11-10 23:00:00 +0000 UTC
// false
}