参考资料: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]