forked from gookit/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml.go
66 lines (50 loc) · 1.59 KB
/
yaml.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
package main
import (
"fmt"
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
)
// go run ./examples/yaml.go
func main() {
config.WithOptions(config.ParseEnv)
// only add decoder
// config.SetDecoder(config.Yaml, yaml.Decoder)
// Or
config.AddDriver(yaml.Driver)
err := config.LoadFiles("testdata/yml_base.yml")
if err != nil {
panic(err)
}
fmt.Printf("config data: \n %#v\n", config.Data())
err = config.LoadFiles("testdata/yml_other.yml")
// config.LoadFiles("testdata/yml_base.yml", "testdata/yml_other.yml")
if err != nil {
panic(err)
}
fmt.Printf("config data: \n %#v\n", config.Data())
fmt.Print("get config example:\n")
name := config.String("name")
fmt.Printf("- get string\n val: %v\n", name)
arr1 := config.Strings("arr1")
fmt.Printf("- get array\n val: %#v\n", arr1)
val0 := config.String("arr1.0")
fmt.Printf("- get sub-value by path 'arr.index'\n val: %#v\n", val0)
map1 := config.StringMap("map1")
fmt.Printf("- get map\n val: %#v\n", map1)
val0 = config.String("map1.key")
fmt.Printf("- get sub-value by path 'map.key'\n val: %#v\n", val0)
// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.String("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))
// set value
config.Set("name", "new name")
name = config.String("name")
fmt.Printf("- set string\n val: %v\n", name)
// if you want export config data
// buf := new(bytes.Buffer)
// _, err = config.DumpTo(buf, config.Yaml)
// if err != nil {
// panic(err)
// }
// fmt.Printf("export config:\n%s", buf.String())
}