新手提问,struct成员首字母小写无法转换成Json

17681880207 · 2017-12-28 07:34:02 · 1150 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2017-12-28 07:34:02 的主题,其中的信息可能已经有所发展或是发生改变。

type Computer struct{
    cpu string
    ram string
    rom string
}
type Computer struct{
    Cpu string
    Ram string
    Rom string
}

为什么上面的写法,没办法转换成Json,下面的首字母大写了却可以啊?


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1150 次点击  
加入收藏 微博
3 回复  |  直到 2017-12-28 13:02:22
buscoop
buscoop · #1 · 7年之前

小写就是private,不可以导出的,这是golang规定的。

3Golds
3Golds · #2 · 7年之前

是的,小写只能内部使用,大写的可以外部调用

jthmath
jthmath · #3 · 7年之前

json包看不到你的包中的私有符号,所以当然解析不出来了,如果想按照小写字母开头的key来解析,可以这样:

type Computer struct {
    Cpu string `json:"cpu"`
    Ram string `json:"ram"`
    Rom string `json:"rom"`
}
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传