dragon4808 2016-10-25 05:38
浏览 4
已采纳

go中的元帅递归类型[重复]

This question already has an answer here:

I want to Marshal and Unmarshal a recursive type in go something like this:

type Dog struct {
    age int
    sibling *Dog
}

Is there any way to do this in golang? I tried with json.Marshal but it doesn't work.

</div>
  • 写回答

1条回答 默认 最新

  • dongqiancui9194 2016-10-25 05:55
    关注

    Your problem is not with recursion, it's understand encapsulation with Golang, e.i. public and private members. In order to encode in Go, your struct has to have public fields (starting with Uppercase):

    type Dog struct {
        Age     int
        Sibling *Dog
    }
    

    Full example: https://play.golang.org/p/eNdLaTfKtN

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word