稍微解释了一下:
```go
// 变量名称(innerFunc) 类型(函数类型) 返回值(函数类型)
func timeSpent(innerFunc func(op int) int) func(op int) int {
// 返回一个函数, 这个函数又一个整形入参 n
return func(n int) int {
// 函数开始执行时间
start := time.Now()
ret := innerFunc(n)
// 打印计算出的 函数执行花费的时间
fmt.Println("Time Spent: ", time.Since(start).Seconds())
// 返回 传入函数(也就是 innerFunc)的返回值
return ret
}
}
// 定义一个符合 timeSpent 入参的函数, 供后面测试
func slowFlow(n int) int {
time.Sleep(time.Second * 1)
return n
}
func TestFn(t *testing.T) {
a, b := returnMultiValues()
t.Log(a, b)
// 调用 timeSpent 函数, 传入 slowFlow 函数
ret := timeSpent(slowFlow)
// 打印返回值
t.Log(ret(10))
}
```
展开