当我使用Unmarshal
Viper 的方法用 Yaml 文件中的值填充我的结构时,所有结构文件仍然是空的,我搜索了很多解决方案,但它们都没有工作:(
我尝试将标签更改mapstructure
为yaml
and structure
,仍然没有工作...
main.go:
type Test struct {
T TConfig `mapstructure:"apiserver"`
}
type TConfig struct {
Address int `mapstructure:"host"`
Port int `mapstructure:"port"`
}
func main() {
var aaa *Test
viper.AutomaticEnv()
viper.AddConfigPath("./")
viper.SetConfigName("config")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
if err := viper.Unmarshal(&aaa); err != nil {
fmt.Println("read config error:", err)
}
fmt.Println("config:", aaa)
}
配置.yml:
apiserver:
- host: localhost
- port: 8080
输出:
read config error: 1 error(s) decoding:
* 'apiserver' expected a map, got 'slice'
config: <nil>
请有人帮我修复错误!