I want to return a type of an interface{}
, while the input value might be var m []*MyModel
I've managed to get to the type *MyModel
, while MyModel
not as a pointer seems to be unreachable to me.
func getType( m interface{} ) reflect.Type {
t := reflect.TypeOf( m );
v := reflect.ValueOf( m );
if t.Kind() == reflect.Ptr {
if v.IsValid() && !v.IsNil() {
return getType( v.Elem().Interface() );
}
panic( "We have a problem" );
}
if t.Kind() == reflect.Slice {
if v.Len() == 0 {
s := reflect.MakeSlice( t , 1 , 1 );
return getType( s.Interface() );
}
return getType( v.Index( 0 ).Interface() );
}
return t;
}
Is it possible?
Aucun commentaire:
Enregistrer un commentaire