且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

learn go function callback

更新时间:2022-08-12 21:12:13

package main

// 参考文档:
//     https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.7.md

import (
    "fmt"
)

func main () {
    callback(1, Add)
}

func Add(a, b int) {
    fmt.Printf("The sum of %d and %d is: %d\n", a, b, a+b);
}

func callback(y int, f func(int, int)) {
    f(y, 2) // this becomes Add(1, 2)
}