-
Notifications
You must be signed in to change notification settings - Fork 9
/
example_test.go
56 lines (46 loc) · 1.04 KB
/
example_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
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
package konf_test
import (
"embed"
"fmt"
"github.com/nil-go/konf"
"github.com/nil-go/konf/provider/env"
kfs "github.com/nil-go/konf/provider/fs"
)
func ExampleGet() {
ExampleSetDefault()
fmt.Print(konf.Get[string]("server.host"))
// Output: example.com
}
func ExampleUnmarshal() {
ExampleSetDefault()
cfg := struct {
Host string
Port int
}{
Host: "localhost",
Port: 8080,
}
if err := konf.Unmarshal("server", &cfg); err != nil {
// Handle error here.
panic(err)
}
fmt.Printf("%s:%d\n", cfg.Host, cfg.Port)
// Output: example.com:8080
}
//go:embed testdata
var testdata embed.FS
func ExampleSetDefault() {
var config konf.Config
if err := config.Load(kfs.New(testdata, "testdata/config.json")); err != nil {
// Handle error here.
panic(err)
}
if err := config.Load(env.New(env.WithPrefix("server"))); err != nil {
// Handle error here.
panic(err)
}
konf.SetDefault(&config)
// Output:
}