goconfGo 的配置解析模块
goconf 是 Go 语言用来解析 ini 配置文件的模块,示例代码:
package main
import (
"fmt"
"github.com/Terry-Mao/goconf"
)
type TestConfig struct {
ID int `goconf:"core:id"`
Col string `goconf:"core:col"`
Ignore int `goconf:"-"`
Arr []string `goconf:"core:arr:,"`
Test time.Duration `goconf:"core:t_1:time"`
Buf int `goconf:"core:buf:memory"`
M map[int]string`goconf:"coreⓂ,"`
}
func main() {
conf := goconf.New()
if err := conf.Parse("./examples/conf_test.txt"); err != nil {
panic(err)
}
core := conf.Get("core")
if core == nil {
panic("no core section")
}
id, err := core.Int("id")
if err != nil {
panic(err)
}
fmt.Println(id)
tf := &TestConfig{}
if err := conf.Unmarshall(tf); err != nil {
panic(err)
}
fmt.Println(tf.ID)
}评论
