By default, mapstructure matches map keys to struct field names case-insensitively. You can customize this using the mapstructure tag.
Renaming Fields
Use the tag to map a specific key to a field:
type User struct {
Username string `mapstructure:"user"` // matches key "user"
}
Squashing Embedded Structs
To treat an embedded struct as if its fields were part of the parent struct (instead of requiring a nested map), use the ,squash suffix:
type Person struct {
Name string
}
type Friend struct {
Person `mapstructure:",squash"` // matches "name" directly in the input map
}
Collecting Remainder Values
To capture all keys that were not mapped to a struct field, use the ,remain suffix on a map field:
type Friend struct {
Name string `mapstructure:"name"`
Other map[string]interface{} `mapstructure:",remain"` // captures everything else
}
Omitting Empty Values
When decoding from a struct to another type, use ,omitempty to skip fields that hold their type's zero value:
type Source struct {
Age int `mapstructure:",omitempty"`
}