mardi 8 décembre 2020

Can I get a variable of a type based on reflect.Type [duplicate]

How do I get convert Value interface{} variable, the original type of which is T reflect.Type, to a variable of T?

If this conversion could be done, I can replace map[string]interface{}+switch value.(type) with this method. I think this is more elegant.

For example

var a int = 1
aType := reflect.TypeOf(a)
type Value struct {
  T reflect.Type          // aType
  OriginValue interface{} // a
}
v := Value{aType, a}
// What should I do to get an integer from v.T and v.OriginValue?




Aucun commentaire:

Enregistrer un commentaire