I try to use unmarshal to struct and It's work great.
And now, I want to unmarshal yaml file to embedded struct. See example.
main.go
type Base struct {
Foo string
}
type SomeStruct struct {
Base
}
func ReturnToStruct(fileName string, dir string) SomeStruct {
viper.SetConfigFile(fileName)
viper.AddConfigPath(dir)
if err := viper.ReadInConfig(); err != nil {
xlog.Fatalf("fatal error config file: %s \n", err)
}
someStruct := SomeStruct{}
if err := viper.Unmarshal(&someStruct); err != nil {
xlog.Fatalf("%v", err)
}
return someStruct
}
test.yml
Foo: asd
When I use
xlog.Infof("%s", someStruct.Foo)
It will return empty string.
Can I use viper with embedded struct ?
Viper actually uses mapstructure.
Try mapstructure:",squash" struct tag on the embedded struct.
@sagikazarmark It work like a charm. Thanks you.
Most helpful comment
Viper actually uses mapstructure.
Try
mapstructure:",squash"struct tag on the embedded struct.