There are two structures A & B. B includes A. Also there is a function attached to A. It returns json of the parent object. I expect to see all object fields in json when I call the fonction on instance of B, but I get only fields of A. Please look at the code:
type A struct {
Foo string
}
type B struct {
A
Bar string
}
func (object *A) toJson() []byte {
res, _ := json.Marshal(&object)
return res
}
func main() {
b := B{}
fmt.Println(string(b.toJson()))
}
I expect to get {"Foo":"", "Bar":""} but the result is {"Foo":""}. The first way is to define two separate functions for both of structures. But is there the second solution with one function? Thank you in advance.