Reputation: 155
Problem at reflection, with MethodByName
Code:
package main
import (
"reflect"
"fmt"
)
type test struct {}
var serviceType = map[string]reflect.Value{
"test": reflect.ValueOf(test{}),
}
func (t *test) prnt() {
fmt.Println("test ok")
}
func callFunc(strct string, fName string) {
s := serviceType[strct].MethodByName(fName)
if !s.IsValid(){
fmt.Println("not correct")
return
}
s.Call(make([]reflect.Value,0))
}
func main() {
callFunc("test", "prnt")
}
Output:
not correct
Playground: https://play.golang.org/p/ZLEQBGYoUOB
Could you help, what I'm doing wrong ?
Upvotes: 2
Views: 1125
Reputation: 3245
Two things needs to be corrected.
prnt
tp Prnt
test
to reflect.ValueOf()
method.Here is the modified working code https://play.golang.org/p/4MK2kqOz6e2
Upvotes: 7