我的 json 配置文件是嵌套的,类似:
{
// Blog global settings
"global": {
"title": "title", // Blog title
"subtitle": "subtitle" // Blog subtitle
},
// Database settings. If you don't know what the value is, leave it empty.
"database": {
"host": "host", // The host name of mongoDB server, could be localhost.
"port": "port", // Port
"database": "database", // Database name
"username": "username",
"password": "password"
},
// Admin users of the blog.
"users": [
{
"username": "username",
"email": "email",
"password": "password"
}
]
}
这样我取出database
的子对象的时候采用:
temp, err := userconf.DIY("database")
if err != nil {
panic(err)
}
dbconf, ok := temp.(config.ConfigContainer)
if !ok {
panic("Configuration file error, can't read database settings.")
}
运行的话就会进入!ok
的 panic,我打印的结果,temp
是一个 map,这样的话是不是就不能再用 config 包来读取dbconf
的配置了?
最新版本现在支持直接
jsonconf.String("database::host")
获取值