-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderstanding-closure.go
30 lines (26 loc) · 1.02 KB
/
understanding-closure.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
// Assign a function that encloses a VARIABLE of TYPE int named x and increments it.
// Wanted to find out why x keeps incrementing, and when and where it is initialised
// Raphael Spannocchi, Learning Go, May 2019
package main
import (
"fmt"
)
func main() {
a := incrementor()
b := incrementor()
fmt.Printf("ANONYMOUS func() int executed. Func location %p, x=%v\n", a, a())
fmt.Printf("ANONYMOUS func() int executed. Func location %p, x=%v\n", a, a())
fmt.Printf("ANONYMOUS func() int executed. Func location %p, x=%v\n", a, a())
fmt.Printf("ANONYMOUS func() int executed. Func location %p, x=%v\n", a, a())
fmt.Printf("ANONYMOUS func() int executed. Func location %p, x=%v\n", b, b())
fmt.Printf("ANONYMOUS func() int executed. Func location %p, x=%v\n", b, b())
}
func incrementor() func() int {
var x int
fmt.Printf("Inside func incrementor, x=%v\nvar x created at location %p\n", x, &x)
return func() int {
fmt.Printf("Inside ANONYMOUS func() int, enclosed x=%v at location %p\n", x, &x)
x++
return x
}
}