-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (55 loc) · 1.21 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
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
package main
import (
"fmt"
"github.com/chenghongxi/tools/config"
)
type YamlConfig struct {
Default YamlDefaultOption `yaml:"default"`
}
type YamlDefaultOption struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}
type JsonConfig struct {
Default JsonDefaultOption `json:"default"`
}
type JsonDefaultOption struct {
Name string `json:"name"`
Age int `json:"age"`
}
type IniConfig struct {
Default IniDefaultOption `ini:"default"`
}
type IniDefaultOption struct {
Name string `ini:"name"`
Age int `ini:"age"`
}
func main() {
// 解析 yaml文件
c := config.New()
c.SetConfigFile("examples/config/yaml/config.yaml")
c.SetConfigType("yaml")
var yaml YamlConfig
if err := c.Binding(&yaml); err != nil {
panic(err)
}
fmt.Println(yaml.Default.Age)
// 解析 json文件
d := config.New()
d.SetConfigFile("examples/config/json/config.json")
c.SetConfigType("json")
var json JsonConfig
if err := c.Binding(&json); err != nil {
panic(err)
}
fmt.Println(json.Default.Age)
// 解析 ini文件
i := config.New()
i.SetConfigFile("examples/config/ini/my.ini")
i.SetConfigType("ini")
var ini IniConfig
if err := c.Binding(&ini); err != nil {
panic(err)
}
fmt.Println(ini.Default.Age)
}