dimanche 13 mars 2016

how do I elegently call a method dynamically in golang?

I'm using reflection to do this:

        method_name := CommandMap[command]
    thing := reflect.ValueOf(self).MethodByName(method_name)
    thing_s := fmt.Sprintf("%s", thing)
    if thing_s == "<invalid reflect.Value>" {
        self.writeMessage(550, "not allowed")
    } else {
        thing.Call([]reflect.Value{})
    }

but there must be a better way. The point of the above code was to get rid of:

if command == "this" {
  runThis()
} else if command == "that" {
  runThat()
} etc. etc.





Aucun commentaire:

Enregistrer un commentaire