问题:
viper读入配置的app.start_time为:start_time=“2020-07-01”(string)
unmarshal到conf结构体中的StartTime(String)字段为空?
且Unmarshal后输出Conf结构体都为类型零值:
结果:
解决方案:
!!!!结构体定义错误!!!!
原来的定义:
type AppConfig struct {
//无论config是什么类型(后缀名称),统一用mapstructure来表示tag
Name string `mapstructure:"name"`
Mode string `mapstructure:"mode"`
Port int `mapdtructure:"port"`
*LogConfig `mapstructure:"log"`
*MysqlConfig `mapstructure:"mysql"`
*RedisConfig `mapstructure:"redis"`
}
解决方案:把AppConfig拆出来,以后访问用:
Conf=new(multipleConfig)
Config.AppConfig.Start_time
var Conf = new(multipleConfig)
type multipleConfig struct {
*AppConfig `mapstructure:"app"`
*LogConfig `mapstructure:"log"`
*MysqlConfig `mapstructure:"mysql"`
*RedisConfig `mapstructure:"redis"`
}
type AppConfig struct {
//无论config是什么类型(后缀名称),统一用mapstructure来表示tag
Name string `mapstructure:"name"`
Mode string `mapstructure:"mode"`
Port int `mapdtructure:"port"`
Version string `mapstructure:"version"`
StartTime string `mapstructure:"start_time"`
MachineId uint16 `mapstructure:"machine_id"`
}