go-tour/function-closures.go

22 lines
303 B
Go
Raw Normal View History

2020-02-05 03:44:36 +00:00
package main
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}