在Go中,为什么我的JSON解码在这里不起作用? [重复]

This question already has an answer here:

I cannot get the standard library's encoding/json package to work for decoding JSON objects. Here's a minimal example:

b := []byte(`{"groups":[{"name":"foo"},{"name":"bar"}]}`)
type Group struct{ name string }
var contents struct {
    groups []Group
}
err := json.Unmarshal(b, &contents)
fmt.Printf("contents = %+v
err = %+v
", contents, err)

This prints:

contents = {groups:[]}
err = nil

But I expect:

contents = {groups:[{name:foo} {name:bar}]}

What am I doing wrong?

</div>

The field names have to start with a capital letter:

type Group struct{ Name string }
var contents struct {
    Groups []Group
}