lundi 30 mars 2020

how to reflect variadic parameter definition in golang

i want the user of the my library to define a function logic that i will fill internally and execute.

// user should define a func like this
func myLogic(name string, age int, text Typex) Result {
    ...
    return Result{...}
}
// then call it like this
lib.run(myLogic)

// the library should implements the run method and call the given function
package lib
// type CustomFunc func(...interface{}) Result
func run(fn func(...interface{}) Result) {
    method := reflect.TypeOf(fn)
    m := reflect.ValueOf(method)
    // params := method.In(0).Elem()
    ???? How do i get the size of fn inputs params
    ???? How do i get the parameter definitions of fn using reflection
}

?? how is it possible in golang





Aucun commentaire:

Enregistrer un commentaire