/
tconf.go
92 lines (82 loc) · 1.76 KB
/
tconf.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"github.com/yylq/config"
)
type MConf struct {
supper Mbase
}
func (m MConf) Supper() Mbase {
return m.supper
}
var mconf = newConf()
func newConf() Mode {
return MConf{supper: NewSupper("conf")}
}
func init() {
fmt.Print("tconf init\n")
ModuleRegisterCase(mconf, "First")
RegisterModule(mconf)
}
func (m MConf) One() {
configFile := "./test.ini"
cfg, err := config.ReadDefault(configFile)
if err != nil {
log.Fatalf("Fail to find:%s err:%v", configFile, err)
}
//set config file std End
var TOPIC = make(map[string]string)
//Initialized topic from the configuration
if cfg.HasSection("main") {
section, err := cfg.SectionOptions("main")
if err == nil {
for _, v := range section {
options, err := cfg.String("main", v)
if err == nil {
TOPIC[v] = options
}
}
}
}
//Initialized topic from the configuration END
fmt.Print(TOPIC)
fmt.Println(TOPIC["debug"])
}
func (m MConf) Tsec() {
type Q struct {
Addr string `config:"main-addr"`
Debug bool `config:"main-debug"`
T1 int32 `config:"other-t1"`
}
configFile := "test.ini"
cfg, err := config.ReadDefault(configFile)
if err != nil {
log.Fatalf("Fail to find:%s err:%v", configFile, err)
}
var q Q
err = cfg.ParseConf(&q)
if err != nil {
log.Fatal(err)
}
log.Println(q)
}
func (m MConf) Two() {
type Q struct {
Runserv []string `config:"main-runserv"`
Runflag []bool `config:"main-runflag"`
Runstatus []int `config:"main-runstatus"`
Runrate []float64 `config:"main-runrate"`
}
configFile := "test.ini"
cfg, err := config.ReadDefault(configFile)
if err != nil {
log.Fatalf("Fail to find:%s err:%v", configFile, err)
}
var q Q
err = cfg.ParseConf(&q)
if err != nil {
log.Fatal(err)
}
log.Println(q)
}