ARTS-S golang函数作为参数传递
函数作为参数传递在单元测试的时候非常有用,看下面的例子。
package main
import "fmt"
func output(f func(string, string, string), fileName string,
got string, right string) {
f(fileName, got, right)
}
func funcA(fileName, got, right string) {
if got == right {
fmt.Println("right")
} else {
fmt.Println("wrong")
}
}
func funcB(fileName, got, right string) {
if got == right {
fmt.Println("right")
} else {
fmt.Println("wrong")
}
}
func main() {
output(funcA, "aaa", "c", "bbb")
output(funcB, "aaa", "bbb", "bbb")
}