go - 在 go 中编码递归类型

标签 go marshalling unmarshalling

<分区>

我想像这样在 go 中编码和取消编码递归类型:

type Dog struct {
    age int
    sibling *Dog
}

有什么办法可以在 golang 中做到这一点吗?我尝试使用 json.Marshal 但它不起作用。

最佳答案

您的问题不在于递归,而是理解 Golang 的封装,e.i.公共(public)和私有(private)成员。 为了在 Go 中进行编码,您的结构必须具有公共(public)字段(以大写字母开头):

type Dog struct {
    Age     int
    Sibling *Dog
}

完整示例:https://play.golang.org/p/eNdLaTfKtN

关于go - 在 go 中编码递归类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232098/

相关文章:

svg - 如何在 Go 中直接包含 SVG 作为 html 部分

c# - 调用采用指针引用的非托管库函数

pointers - 解码嵌套结构和类型断言

yaml - 使用环境变量设置 bool 字段时发生 Rancher 错误

dictionary - 如何正确初始化一片 map

go - golang中接口(interface)指针和接口(interface)值有什么区别

python - Sublime Text 3 - 语言特定的 Goto 定义键盘快捷键

json - 如何从bson文档中呈现json字符串

c# - 从 C# 调用 Delphi 函数

java - 将缺少的 xml 属性解码到可选的 java 对象