mitchellh / mapstructure

Go library for decoding generic map values into native Go structures and vice versa.
https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc
MIT License
7.93k stars 677 forks source link

regarding map decoding #352

Closed saiteja2021 closed 9 months ago

saiteja2021 commented 9 months ago

i wanted to omit the fields which are not part of my struct. but i was getting below error while running the below code. is there any thing i am miss here?

1 error(s) decoding:

* '' has invalid keys: test
type MyStruct struct {
    Name string
    Age  int
}
map1 := map[string]interface{}{
        "name": "test",
        "age":  30,
        "test": "test",
    }

    // Create a new struct to hold the data
    var data MyStruct

    // Use mapstructure to map the data from the map to the struct
    config := &mapstructure.DecoderConfig{
        ErrorUnused: true,
        Result:      &data,
    }
    decoder, err := mapstructure.NewDecoder(config)
    if err != nil {
        fmt.Println(err)
        return
    }
    if err := decoder.Decode(map1); err != nil {
        fmt.Println(err)
        return
    }
saiteja2021 commented 9 months ago

i have got the solution