在 go 中编组递归类型

Marshal recursive types in go

我想像这样编组和取消编组递归类型:

type Dog struct {
    age int
    sibling *Dog
}

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

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

type Dog struct {
    Age     int
    Sibling *Dog
}

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