mardi 30 juillet 2019

Golang - get a package and method dynamically from a string value [duplicate]

This question already has an answer here:

I come from the Java-side of things where I could easily get a class from a string and the method in the class also from a string using reflection.

I'd like to do the same thing in golang and from the looks of the specification on golang, I won't be able to do the same thing:

Essentially I want to do:

// keep in mind this is pseudo-code
func Get(className string, methodName string, parameters ...interface{}) {
  class, err := reflect.GetType(className)
  // ... typical error checking

  method, err = reflect.GetMethod(class, "methodName")
  // ... typical error checking

  method.Call(parameters)
}

I have a configuration file which may have many implementations for a particular interface. In it, I specify the type and method it should use to load and create the object.

All of the reflect code I saw was referencing having the interface upfront, ie. not from a string.

Is this possible?





Aucun commentaire:

Enregistrer un commentaire