EDIT
: I feel this question is not a duplicate, as the referenced Q&A do not describe JSON in the use cases.
I have a struct
nested within another struct
that I use for JSON operations.
In a simple test case, using a copy of a nested struct:
type A struct {
Foo string
Bar B
}
type B struct {
Baz string
}
func main() {
serialized := `{"foo": "some", "bar": {"baz": "thing"}}`
a := &A{}
json.Unmarshal([]byte(serialized), a)
fmt.Println(a.Foo, a.Bar.Baz)
}
> some thing
yields the same results as using a pointer to a nested struct:
type A struct {
Foo string
Bar *B
}
type B struct {
Baz string
}
func main() {
serialized := `{"foo": "some", "bar": {"baz": "thing"}}`
a := &A{}
json.Unmarshal([]byte(serialized), a)
fmt.Println(a.Foo, a.Bar.Baz)
}
> some thing
When would or would I not want to use a pointer?