I am trying to do this:
type S struct {
Name string
Children []interface{}
}
func main() {
s := S{Name: "Bob", Children: []interface{}{}}
fmt.Println("%v", s)
s.Children = append(s.Children, "Tom")
fmt.Println("%v", s)
// How do I do the above line with reflect? To add "Jane"?
c := reflect.ValueOf(s).FieldByName("Children")
newSlice := reflect.Append(c, reflect.ValueOf("Jane"))
reflect.ValueOf(s).FieldByName("Children").Set(newSlice)
fmt.Println("%v", s)
}
But I am getting the error:
panic: reflect: reflect.Value.Set using unaddressable value
What am I doing wrong?