mardi 24 janvier 2017

Call method by name via reflection api in golang

I have multiple packages and want to call methods of these packages dynamically based on the configuration through reflection api.

Code Sample

Package abc

package abc

func Detect(msg string) bool {
  return true
}

package xyz

package xyz

func Detect(msg string) bool {
  return true
}

Main

func main(){
  reflect.ValueOf("<package_name>").MethodByName("Detect").Call([]reflect.Value{reflect.ValueOf("hello")})
}

When I execute the above code, I have got the following error.

Output

panic: reflect: call of reflect.Value.Call on zero Value

Any help would be appreciated :)





Aucun commentaire:

Enregistrer un commentaire