-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
91 lines (78 loc) · 1.58 KB
/
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
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2022, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"fmt"
"net"
"net/url"
"time"
"github.com/davecgh/go-spew/spew"
)
// Below example demonstrates how to decode system environment variables into a
// struct.
func ExampleDecoder_Decode() {
type Config struct {
Foo string
Timeout time.Duration `default:"10s"`
}
var conf Config
if err := NewDecoder(System()).Decode(&conf); err != nil {
panic(err)
}
spew.Dump(conf)
// Output:
// (env.Config) {
// Foo: (string) "",
// Timeout: (time.Duration) 10s
// }
}
func ExampleUnmarshal() {
type Envs struct {
Foo string
Bar struct {
Url url.URL
} `env:",inline"`
Timeout time.Duration `default:"10s"`
Ip net.IP
}
var data = `
FOO=bar
# ignore me
URL=http://example.com
IP=192.168.1.1`
var envs Envs
if err := Unmarshal([]byte(data), &envs); err != nil {
panic(err)
}
spew.Dump(envs)
// Output:
// (env.Envs) {
// Foo: (string) (len=3) "bar",
// Bar: (struct { Url url.URL }) {
// Url: (url.URL) http://example.com
// },
// Timeout: (time.Duration) 10s,
// Ip: (net.IP) (len=16 cap=16) 192.168.1.1
// }
}
func ExampleMarshal() {
type Envs struct {
Foo string
Bar struct {
Url url.URL `default:"https://example.com"`
} `env:",inline"`
Timeout time.Duration `default:"10s"`
Ip net.IP
}
b, err := Marshal(Envs{})
if err != nil {
panic(err)
}
fmt.Println(string(b))
// Output:
// FOO=
// URL=https://example.com
// TIMEOUT=10s
// IP=
}