Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshal recursive types in go [duplicate]

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.

like image 228
Ross Avatar asked Oct 25 '25 05:10

Ross


1 Answers

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

like image 191
Yandry Pozo Avatar answered Oct 28 '25 05:10

Yandry Pozo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!