I want to test a given function in runtime, to verify that it matches a certain signature. It's sufficient that this signature complies with some interface, not a specific implementation.
I can't seem to find the right pattern to do such validation. In runtime, the function's type is using the implementation.
How can I compare the function's signature to the interface?
package main
import "fmt"
type myinteface interface{
DoSomething(int) string
}
type myfunc func(myinteface)
type impl struct {}
func (im *impl) DoSomething(int) string{
return "fsdfsd"
}
func do(i interface{}) {
switch v := i.(type) {
case func(myinteface):
fmt.Print("doesn't stop here")
case func(impl):
fmt.Print("this does work")
default:
fmt.Printf("I don't know about type %T!
", v)
}
}
func test(im impl) {}
func main() {
do(test)
}
see it live in playground