go语言实现回调函数

来源:互联网 发布:文件管理网站源码 编辑:程序博客网 时间:2024/06/14 22:25

定义:回调函数就是一个通过函数指针调用的函数。

package main
import "fmt"
type Callback func(x, y int) int
func testCallback(x, y int, callback Callback) int {
return callback(x, y)
}
func add(x, y int) int {
return x + y
}
func minus(x, y int) int {
return x - y
}
func main() {
fmt.Println(testCallback(4, 5, add))
fmt.Println(testCallback(4, 5, minus))
}

结果:

9

-1

成功: 进程退出代码 0.





原创粉丝点击