I want to define a function to run a callback function, and the callback function will be uncertain, such as args or returns both are uncertain.
I try this:
package main
import (
"fmt"
)
func F(callback func(args ...interface{}) interface{}, args ...interface{}) {
rev := callback(args...)
fmt.Println(rev)
}
func CB1() {
fmt.Println("CB1 called")
}
func CB2() bool {
fmt.Println("CB2 called")
return false
}
func CB3(s string) {
fmt.Println("CB3 called")
}
func CB4(s string) bool {
fmt.Println("CB4 called")
return false
}
func main() {
F(CB1)
F(CB2)
F(CB3, "xxx")
F(CB4, "yyy")
}
errors:
./play.go:31:3: cannot use CB1 (type func()) as type func(...interface {}) interface {} in argument to F
./play.go:32:3: cannot use CB2 (type func() bool) as type func(...interface {}) interface {} in argument to F
./play.go:33:3: cannot use CB3 (type func(string)) as type func(...interface {}) interface {} in argument to F
./play.go:34:3: cannot use CB4 (type func(string) bool) as type func(...interface {}) interface {} in argument to F