Marshal嵌入式结构体

huangapple go评论76阅读模式
英文:

Marshal Embedded Struct

问题

我在玩耍学习嵌入是如何工作的。http://play.golang.org/p/oHOim4G1-l

当我编组Child结构时,它会编组为{}。为什么会这样编组?

英文:

I was playing around to learn how embedding works. http://play.golang.org/p/oHOim4G1-l

When I marshalled Child struct it marshals as {}. Why does it marshal like this?

答案1

得分: 2

你的JSON字典为空,因为结构体的字段(或结构体内嵌的任何结构体)都没有被导出。

如果你将字段名改为以大写字母开头,那么encoding/json模块就能够识别它们。当然,由于你还有名为NameValue的方法,你需要将它们改为其他名称以避免冲突。

英文:

Your JSON dictionary is empty because none of the fields of the struct (or any of the structures embedded inside the struct) are exported.

If you change the field names to start with an upper case letter, then the encoding/json module will be able to see them. Of course since you've also got methods called Name and Value, you'll need to call them something else to avoid a conflict.

huangapple
  • 本文由 发表于 2015年7月24日 07:35:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/31599836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定