回调函数

回调函数

  1. 概念

    高阶函数:可以将一个函数作为另一个函数的参数

    //fun1(),fun2()
    //将fun1函数作为fun2函数的参数
    
    //那么fun2就叫做高阶函数
    //fun1叫做回调函数
    
    //高阶函数
    func opt(a,b int,f func(int,int) int) int {
    	fmt.Printf("%T
    ",f)
    	fmt.Println(f)
    	return f(a,b)
    }
    //回调函数
    func add(a,b int) int {  
    	return a+b
    }
    //传入函数引用
    res := opt(1,2,add)
    
    
    //通过匿名函数方式
    mult := func(a,b int) int {
        return a*bb
    }
    //传入匿名函数
    res = opt(2,3,mult)
    
    //直接在高阶函数调用时定义匿名函数
    res = opt(4,5,func(a,b int)int{
        return a/b
    })
    
原文地址:https://www.cnblogs.com/henryno12/p/12655701.html