英文:
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
模块就能够识别它们。当然,由于你还有名为Name
和Value
的方法,你需要将它们改为其他名称以避免冲突。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论