mercredi 29 janvier 2020

reflection - get type of single slice element

I want to iterate over struct definition recursively and for slices get a type of a singular element. Then, create an empty instance of that type. For example:

type Path struct {
  Name string
  Points []Coordinate
}
type Coordinate struct {
  Latitude float64
  Longitude float64
}

Assuming that types are unknown at runtime, how can I create an empty instance of the nested type (in the above example Coordinate). I mean:

x := Coordinate{}

When at input I get Path (which can be any other struct, with slices of different types)?





Aucun commentaire:

Enregistrer un commentaire