元帅嵌入式结构 [英] Marshal Embedded Struct

查看:78
本文介绍了元帅嵌入式结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩耍以了解嵌入如何工作。 http://play.golang.org/p/oHOim4G1-l



当我编组Child时,将它编组为 {} 。为什么它名帅这样?


解决方案

您JSON字典是空的,因为没有一个结构的字段(或任何嵌入在结构内部结构)是导出



如果您将字段名称更改为以大写字母开头,那么编码/ json 模块将能够看到它们。当然,因为你也有一些称为名称的方法,所以你需要给它们一些东西避免冲突。


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?

解决方案

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.

这篇关于元帅嵌入式结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆