在Go中解组json时可以访问“额外”字段吗?

Lets say I have this type:

type Foo struct{
   Bar string `json:"bar"`
}

and I want to unmarshal this json into it:

in := []byte(`{"bar":"aaa", "baz":123}`)

foo := &Foo{}
json.Unmarshal(in,foo)

will succeed just fine. I would like to at least know that there were fields that were skipped in the processing. Is there any good way to access that information?

playground snippet

As you're probably aware you can unmarshal any valid json into a map[string]interface{}. Having unmarsahled into an instance of Foo already there is no meta data available where you could check fields that were excluded or anything like that. You could however, unmarshal into both types and then check the map for keys that don't correspond to fields on Foo.

in := []byte(`{"bar":"aaa", "baz":123}`)

foo := &Foo{}
json.Unmarshal(in,foo)

allFields := &map[string]interface{}
json.Unmarshal(in, allFields)
for k, _ := range allFields {
    fmt.Println(k)
    // could also use reflect to get field names as string from Foo
    // the get the symmetric difference by nesting another loop here
    // and appending any key that is in allFields but not on Foo to a slice
}