Golang 多格式配置文件加载.
$ go get -u github.com/usthooz/gconf
- Golang配置文件加载
- .json
- .toml
- .xml
- .yaml
import (
"github.com/usthooz/gconf"
"github.com/usthooz/oozlog/go"
)
type Config struct {
Author string
Mysql struct {
User string
Password string
}
}
func main() {
var (
conf Config
)
// new conf object
ozconf := gconf.NewConf(&gconf.Gconf{
ConfPath: "./config.json", // 可选,默认为./config/config.yaml
Subffix: "", // 可选,如果不指定则自动解析文件名获取
})
// get config
err := ozconf.GetConf(&conf)
if err != nil {
uoozg.Errorf("GetConf Err: %v", err.Error())
}
uoozg.Infof("Res: %v", conf)
}
- 输出
➜ example git:(master) ✗ go run example.go
2018/09/06 12:20:10 [INFO] Res: [{ooz {ooz 123}}] [logger.go:101]