package main
import "fmt"
func adder() func(int) int{
//sum的值可以改变
sum:=0
return func(v int) int {
sum+=v
return sum
}
}
//正统函数式
type adder1 func(int) (int,adder1)
func adder2(base int) adder1{
return func(v int) (int, adder1){
return base+v, adder2(base+v)
}
}
func main() {
a:=adder()
for i:=0;i<10;i++{
fmt.Println(a(i))
}
b:=adder2(0)
for j:=0;j<10;j++{
var s int
s, b = b(j)
fmt.Println(j,s)
}
}
有疑问加站长微信联系(非本文作者)