-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-defer.go
48 lines (42 loc) · 994 Bytes
/
simple-defer.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
41
42
43
44
45
46
47
48
package main
import (
"fmt"
)
// The `defer` keyword postpones the execution of a function
// until the surrounding function returns
// It is very important to remember that
// deferred functions are executed in Last In First Out (LIFO) order
// after the return of the surrounding function.
func d1() {
for i := 3; i > 0; i-- {
defer fmt.Print("d1-", i, " ")
}
}
func d2() {
// The tricky part here is that the deferred anonymous function
// is evaluated after the for loop ends, because it has no parameters.
// This means that is evaluated three times for an i value of 0.
for i := 3; i > 0; i-- {
defer func() {
fmt.Print("d2-", i, " ")
}()
}
fmt.Println()
}
func d3() {
// the best approach to the use of `defer`
// all the parameters passed to the anonymous function
// is evaluated at the call of `defer`
for i := 3; i > 0; i-- {
defer func(n int) {
fmt.Print("d3-", n, " ")
}(i)
}
}
func main() {
d1()
d2()
fmt.Println()
d3()
fmt.Println()
}