Here you have two closures with shared state. Unsure whether that qualifies…
Why wouldn’t it? createCounter could return one closure to increment c
, but this example shows how one can group closures together to create an object with shared state.
Yea, but now you have a “group”.
Perhaps you were thinking about something like (Go Playground):
func NewCounter() func(int) int {
var coutnter int
return func(x int) int {
coutnter += x
return coutnter
}
}
func main() {
c := NewCounter()
fmt.Println("+1:", c(1))
fmt.Println("+2:", c(2))
fmt.Println("-1:", c(-1))
}