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

In Go, why is my JSON decoding not working here?

我无法使用标准库的 encoding/json 包来解码 JSON 对象。这是一个最小的例子:

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\nerr = %+v\n", contents, err)

这会打印:

contents = {groups:[]}
err = nil

但我预计:

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

我做错了什么?

字段名称必须以大写字母开头:

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