golang multiconfig 示例

参考资料:https://github.com/koding/multiconfig

测试代码:

package main

import (
        "fmt"
        "github.com/koding/multiconfig"
)

type Server struct {
        Demo DemoConfig
}

type DemoConfig struct {
        Name    string
        Port    int
        Enabled bool
        Users   []string
}

func main() {
        fmt.Println("Hello, World!")
        m := multiconfig.NewWithPath("config.toml") // supports TOML, JSON and YAML

        serverConf := new(Server)
        err := m.Load(serverConf) // Check for error
        if err != nil {
                fmt.Println(err)
        }
        m.MustLoad(serverConf) // Panic's if there is any error

        fmt.Println(serverConf.Demo.Port)
        fmt.Println(serverConf.Demo.Name)
        fmt.Println(serverConf.Demo.Users)
}

配置文件:

[root@localhost multiconfig]# cat config.toml 
[Demo]
Port = 1000
Name = "test"
Users = ["aaa", "bbb", "ccc"]

运行结果:

[root@localhost multiconfig]# go run test.go 
Hello, World!
1000
test
[aaa bbb ccc]

 

posted @ 2020-07-20 11:48  salami_china  阅读(720)  评论(0编辑  收藏  举报