package main import ( "bytes" "encoding/json" ) type Person struct { Name string Age int Friend *Person } func main() { friend := Person{"john", 40, nil} person := Person{"paul", 71, &friend} var buf bytes.Buffer b, _ := json.Marshal(person) buf.Write(b) println(buf.String()) }
{"Name":"paul","Age":71,"Friend":{"Name":"john","Age":40,"Friend":null}}