【发布时间】:2019-11-08 11:15:01
【问题描述】:
当我使用 viper 的 Unmarshal 方法用我的 yaml 文件中的值填充我的配置结构时,一些结构字段变为空!
我是这样做的:
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.AddConfigPath("/etc/myapp/")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
// error checking ...
conf := &ConfYaml{}
err = viper.Unmarshal(conf)
// error checking ...
我的结构是这样的:
type ConfYaml struct {
Endpoints SectionStorageEndpoint `yaml:"endpoints"`
}
type SectionStorageEndpoint struct {
URL string `yaml:"url"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
UseSSL bool `yaml:"use_ssl"`
Location string `yaml:"location"`
}
这里url和location字段在yaml文件中填充了正确的值,但其他字段为空!
奇怪的是,当我尝试打印如下字段时:
viper.Get("endpoints.access_key")
它在yaml文件中打印正确的值并且不为空!!
【问题讨论】:
-
您能否提供一个完整可验证的示例,并附上实际输出和您的预期?
-
@Flimzy 是的,我更新了问题,希望够了。
-
所以你认为它是空的?
-
@Flimzy 不,我希望所有字段都用 yaml 文件中的值填充。但是某些值(例如 url 和 location)已填充,而其他值则为空!当我用 viper.Get() 打印空的时,它们不是空的!所以解组方法对我来说不能正常工作。