FFIVE
Go 函数是一流的值。您不需要恢复到动态语言的技巧。package mainimport "fmt"func someFunction1(a, b int) int { return a + b}func someFunction2(a, b int) int { return a - b}func someOtherFunction(a, b int, f func(int, int) int) int { return f(a, b)}func main() { fmt.Println(someOtherFunction(111, 12, someFunction1)) fmt.Println(someOtherFunction(111, 12, someFunction2))}输出:12399如果函数的选择取决于某些仅运行时已知的值,则可以使用映射:m := map[string]func(int, int) int { "someFunction1": someFunction1, "someFunction2": someFunction2,}...z := someOtherFunction(x, y, m[key])