samedi 10 septembre 2022

How to get a single struct with a struct array in reflect

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.





Aucun commentaire:

Enregistrer un commentaire