goconfGo 的配置解析模块

联合创作 · 2023-09-28 16:02

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)
}
浏览 8
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报