go callback function & anonymous function

Posted Jun 7, 20201 min read

package main

import "fmt"

//go callback function
func main() {

    /*
     Anonymous:no name
       Anonymous objects:
       Anonymous function:the function has no name

       It can only be used once. Use directly when defining
    */
    fun1 := func(a, b int) int {
        fmt.Println("a:", a, "b:", b)
        return a * b
    }
    res2 := oper(1, 2, fun1)
    fmt.Println(res2) //3
}

func oper(m, n int, fun func(int, int) int) int {

    return fun(1, 2)
}