-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_test.go
68 lines (56 loc) · 1.08 KB
/
doc_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
package fromenv
import (
"fmt"
"net/url"
"os"
"time"
)
func Example() {
var c struct {
Field1 string `env:"KEY1"`
}
os.Setenv("KEY1", "foo")
_ = Unmarshal(&c)
fmt.Println(c.Field1)
// Ouput: foo
}
func Example_default() {
var c struct {
Field1 string `env:"KEY1=key1default"`
}
os.Unsetenv("KEY1")
_ = Unmarshal(&c)
fmt.Println(c.Field1)
// Output: key1default
}
func Example_inner() {
var c struct {
Inner struct {
Field2 string `env:"KEY2"`
}
}
os.Setenv("KEY2", "inner too")
_ = Unmarshal(&c)
fmt.Println(c.Inner.Field2)
// Output: inner too
}
func ExampleSetFunc() {
durSetter := func(t *time.Duration, s string) error {
x, err := time.ParseDuration(s)
*t = x
return err
}
urlSetter := func(u *url.URL, s string) error {
x, err := url.Parse(s)
*u = *x
return err
}
type config struct {
Timeout time.Duration `env:"GAP=1000ms"`
Server *url.URL `env:"PLACE=http://www.github.com"`
}
var c config
_ = Unmarshal(&c, SetFunc(durSetter), SetFunc(urlSetter))
fmt.Println(c.Timeout, c.Server.Hostname())
// Output: 1s www.github.com
}