Right here you’ve two closures with shared state. Uncertain whether or not that qualifies…
Why wouldn’t it? createCounter may return one closure to increment c
, however this instance reveals how one can group closures collectively to create an object with shared state.
Yea, however now you’ve a “group”.
Maybe you have been eager about one thing like (Go Playground):
func NewCounter() func(int) int {
var coutnter int
return func(x int) int {
coutnter += x
return coutnter
}
}
func essential() {
c := NewCounter()
fmt.Println("+1:", c(1))
fmt.Println("+2:", c(2))
fmt.Println("-1:", c(-1))
}