Golang 结构字段的名称和解组到这个结构中

Golang structure field's name and unmarshalling into this structure

我写了一个示例程序来说明我的问题,可以在这里查看: https://play.golang.org/p/6776lYcbBR

所以我的问题是:

当结构(GameOne)字段的名称以大写字母开头时,json.Unmarshal 按预期工作; 当它以小写字母开头时 (GameTwo),字段值设置为默认值。

为什么会这样?跟scope/visibility规则有关系吗?

提前致谢。

json.Unmarshal 仅设置结构中的导出字段,并且对于导出字段,首字母必须大写。
有关详细信息,我强烈建议您查看 documentation

来自 the documentation(强调已添加):

Unmarshal will only set exported fields of the struct.

当然不会导出以小写字母开头的字段。所以 JSON 封送拆收器(或者你的包之外的任何东西)都无法影响它们。