vendredi 4 janvier 2019

Go get return value of a reflected function call

I'm trying to create a function which calls another one based on it's name passed as a string, here it is

func invokeFunction(name string, args ...interface{}) error {
  inputs := make([]reflect.Value, len(args))
  for i := range args {
    inputs[i] = reflect.ValueOf(args[i])
  }
  s := StructWithFunctions{}
  f := reflect.ValueOf(s).MethodByName(name)
  if !f.IsValid() {
    return errors.New("There is no function with that name")
  }
  err := f.Call(inputs)
  return err 
}

I made sure all the functions from StructWithFunctions return an error, my problem is in the return

cannot use err (type []reflect.Value) as type error in return argument:
    []reflect.Value does not implement error (missing Error method)go

It seems like I have to make a cast somewhere, but I'm completely lost about where to do so.

Thanks!





Aucun commentaire:

Enregistrer un commentaire