79282155

Date: 2024-12-15 10:39:59
Score: 1
Natty:
Report link

Use reflection with interface, that way you will be able to use any function with interface. I made some changes and tested it on playground

func main() {
var foo interface{}
foo = log.Printf
dynamicFunction(foo, "Pointer, %s!", "Test")}

func dynamicFunction(fn interface{}, args ...interface{}) {

v := reflect.ValueOf(fn)
in := make([]reflect.Value, len(args))
for i, arg := range args {
    in[i] = reflect.ValueOf(arg)
}
v.Call(in)}

Playground

Reasons:
  • Probably link only (1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: saif iqbal