forked from knadh/koanf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
36 lines (29 loc) · 1.09 KB
/
main.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
package main
import (
"fmt"
"log"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
)
// Global koanf instance. Use "." as the key path delimiter. This can be "/" or any character.
var k = koanf.New(".")
func main() {
// Load default values using the confmap provider.
// We provide a flat map with the "." delimiter.
// A nested map can be loaded by setting the delimiter to an empty string "".
k.Load(confmap.Provider(map[string]interface{}{
"parent1.name": "Default Name",
"parent3.name": "New name here",
}, "."), nil)
// Load JSON config on top of the default values.
if err := k.Load(file.Provider("mock/mock.json"), json.Parser()); err != nil {
log.Fatalf("error loading config: %v", err)
}
// Load YAML config and merge into the previously loaded config (because we can).
k.Load(file.Provider("mock/mock.yaml"), yaml.Parser())
fmt.Println("parent's name is = ", k.String("parent1.name"))
fmt.Println("parent's ID is = ", k.Int("parent1.id"))
}