This question already has an answer here:
Suppose I have a program like below
package main
import "fmt"
func main() {
Execute(1,One)
// Execute(2,Two)
// Execute(3,Three)
}
type Executable func(int)
func Execute(noOfArgs int, fn Executable){
switch noOfArgs {
case 1 : fn(1)
// case 2 : fn(1,2)
// case 3 : fn("1",2,3)
}
}
func One(n int) {
fmt.Println("Foo = ",n)
}
func Two(n1,n2 int) {
fmt.Println("Foo = ",n1+n2)
}
func Three(n1 string,n2,n3 int) {
fmt.Println("Foo = ",n1+n2+n3)
}
And I would like to make the Execute
function as a generic one which can receive functions with different number of arguments of different types, what should be the type of Executable
?
In other words, If I uncomment the commented lines in the above program, it will break. What modification should I make to the line type Executable func(int)
to make it working?
PS : Please try to give a generic answer instead of giving a workaround for the exact scenario which I mentioned
Aucun commentaire:
Enregistrer un commentaire