I have a data structure like this demo.
type Family struct {
first string
last string
}
type Person struct {
name string
family *Family
}
func main(
As you've noted, family
is *Family
. And as the error says, you cannot call .FieldByName(...)
on a reflect.Value
where that value is a pointer.
Instead you need to indirect the pointer, to get the value that it points to, and call .FieldByName(...)
on that.
familyPtr := v.FieldByName("family")
v = reflect.Indirect(familyPtr).FieldByName("last")
See docs on indirect: https://golang.org/pkg/reflect/#Indirect