I am trying to create a function, which acts as a proxy for executing a function.
It accepts a pointer to a value (which will be where the result is set), a func and a number of params. It executes the function and applies the result on the value.
My first challenge is executing the function as the declared type is not a func
(and can't be). think I can achieve this using reflect.MakeFunc
, but I am having no success.
An example of what I am trying to achieve below.
package main
import "fmt"
// Execute the function which returns this struct
type item struct {
key string
value string
otherValue string
}
func todo(param string) (*item, error) {
return &item{
key: param,
}, nil
}
type BindFunc struct {
Params func(params ...interface{}) *BindFunc
Results func(results ...interface{}) *BindFunc
Do func()
}
// NewBindFunc is a proxy function should run the function and bind the results
// to the result
func NewBindFunc(fn interface{}) *BindFunc {
b := &BindFunc{}
b.Params = func(params ...interface{}) *BindFunc {
return b
}
b.Results = func(results ...interface{}) *BindFunc {
return b
}
b.Do = func() {
// execute the function
}
return b
}
func main() {
var result item
var err error
NewBindFunc(todo).
Params("Param").
Results(&result, &err).
Do()
fmt.Println(result.key)
}