I have one reflect.Type and I need to call MethodByName
. If the method is defined on type T
, no problem to find it. If it's defined on *T
, this mehtod will return invalid value. So I tried to convert T
to *T
and failed. Here is what I do:
First, I tried to create a new value from the type. Type infomation seems to lost after creation.
t := reflect.TypeOf(src) // src is interface{} type
mt, exists := t.MethodByName(name)
if !exists {
el := reflect.New(t)
t = reflect.TypeOf(el)
mt, exists = t.MethodByName(name)
fmt.Println(t, mt, exists)
}
Then, I tried to get type directly from src(the interface type), and failed also.
t := reflect.TypeOf(src)
mt, exists := t.MethodByName(name)
if !exists {
t = reflect.TypeOf(&src) // *interface{} type, not what I want
mt, exists = t.MethodByName(name)
fmt.Println(t, mt, exists)
}
Aucun commentaire:
Enregistrer un commentaire