package main
import (
"fmt"
)
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, reg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
i, //sum[i] = i
pos(i), //pos(i) = pos(i).sum + i
reg(-2*i), //reg(-2 * i) = reg(i) + (-2*i)
)
}
}
输出:
API server listening at: 127.0.0.1:26756
0 0 0
1 1 -2
3 2 -6
6 3 -12
10 4 -20
15 5 -30
21 6 -42
28 7 -56
36 8 -72
45 9 -90
有疑问加站长微信联系(非本文作者)