1
type Foo struct {
    Bar interface{}
    Nothing string
}

type A struct {
    ...
}
type B struct {
    ...
}

var car Foo

In the program, car.Bar maybe []A or []B.

I don't know what is real 'Foo' before the program running, when I use this:

reflect.New(reflect.TypeOf(car).Field(1)) // here I can get a array []A or []B's type only

But now I have to append a new object to car.Bar. Help me, how do I append this, I can't find a function in reflect doc anymore.

wan9xy
  • 11
  • 1
  • Use type assertion or type switch to detect which one is it, do the append, and reassign the result. – icza Sep 09 '22 at 08:40

0 Answers0