-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
52 lines (40 loc) · 909 Bytes
/
Copy pathexample_test.go
File metadata and controls
52 lines (40 loc) · 909 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
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
package configutil_test
import (
"fmt"
"os"
"github.com/h-dav/configutil"
)
func ExampleSet() {
type Config struct {
Value string `config:"VALUE"`
}
if err := os.Setenv("VALUE", "value"); err != nil {
panic(err)
}
defer func() { _ = os.Unsetenv("VALUE") }()
var cfg Config
if err := configutil.Set(&cfg); err != nil {
panic(err)
}
fmt.Println(cfg.Value)
// Output:
// value
}
func ExampleSet_advanced() {
type Config struct {
Service string `config:"SERVICE,required"`
Port int `config:"PORT,default=8080"`
}
if err := os.Setenv("SERVICE", "auth"); err != nil {
panic(err)
}
defer func() { _ = os.Unsetenv("SERVICE") }()
// PORT is not set, so it will use the default value.
var cfg Config
if err := configutil.Set(&cfg); err != nil {
panic(err)
}
fmt.Printf("Service: %s, Port: %d\n", cfg.Service, cfg.Port)
// Output:
// Service: auth, Port: 8080
}