Go函数(三)之回调函数

本文仅是自己阅读笔记,不正确之处请多包涵和纠正。
原文The way to go

函数可以作为其它函数的参数进行传递,然后在其它函数内调用执行,一般称之为回调。

package main

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)
}

输出:

The sum of 1 and 2 is: 3

你可能感兴趣的:(Golang)