我用毒蛇。我正在尝试从具有yml配置的结构中获取信息。
type Config struct {
Account User `mapstructure:"user"`
}
type User struct {
Name string `mapstructure:"name"`
Contacts []Contact `mapstructure:"contact"`
}
type Contact struct {
Type string `mapstructure:"type"`
Value string `mapstructure:"value"`
}
func Init() *Config {
conf := new(Config)
viper.SetConfigType("yaml")
viper.ReadInConfig()
...
viper.Unmarshal(conf)
return conf
}
...
config := Init()
...
for _, contact := range config.Account.Contacts {
type := contact.type
vlaue := contact.value
}
和配置
user:
name: John
contacts:
email:
type: email
value: test@test.com
skype:
type: skype
value: skypeacc
我可以获得这样的结构项目吗?我无法获得这样的联系人数据。可能吗?
侃侃无极
潇湘沐
青春有我
随时随地看视频慕课网APP
相关分类