-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (35 loc) · 784 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package closures
import "fmt"
/*
Go supports anonymous functions, which can form `closures`.
Closures are instances of a function, a value whose non local variables
have been bound either to values or to storage locations.
*/
func Run() {
seq := intSequence()
// i started at 100, each call increments i
for i := 0; i < 100; i++ {
fmt.Println(seq())
/*
101,
102,
...,
199,
200
*/
}
// To prove out the 'state' is contained to the function instance:
another := intSequence()
fmt.Println(another()) // 101
}
/*
This functions returns another function, which is anonymously defined
in the body. The returned function `closes over` the variable i to form a closure
*/
func intSequence() func() int {
i := 100
return func() int {
i++
return i
}
}