指针

回调函数

package main

import "fmt"

// 把匿名函数作为另一个函数的参数,就叫做回调函数
//设计一个函数,求2个数的加减乘除

func add(a, b int) int { //add: func(int, int) int  //普通函数
	return a + b
}
func oper(a, b int, fun func(int, int) int) int { //oper: func(int, int, func(int, int) int) int //这个就叫高阶函数
	fmt.Println(a, b, fun) //打印3个参数
	res := fun(a, b)
	return res
}
func main() {
	fmt.Printf("add: %T\n", add)
	fmt.Printf("oper: %T\n", oper)

	///////////
	//add普通函数调用
	res1 := add(1, 2)
	fmt.Println(res1)

    ///////////
	//回调函数调用
	res2 := oper(10, 20, add)
	fmt.Println(res2)

}

高阶函数解释:

image-20230127170152846

posted @ 2023-01-27 17:06  john5的博客  阅读(5)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css