-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.go
61 lines (46 loc) · 950 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"time"
)
func main() {
// single()
// stacked()
findTheMeaning()
}
func findTheMeaningNoDefer() {
start := time.Now()
fmt.Printf("%s starts...\n", "findTheMeaning")
// do some heavy calculation...
time.Sleep(time.Second * 2)
fmt.Printf("%s took %v\n", "findTheMeaning", time.Since(start))
}
func findTheMeaning() {
defer measure("findTheMeaning")()
// do some heavy calculation
time.Sleep(time.Second * 2)
}
func measure(name string) func() {
start := time.Now()
fmt.Printf("%s starts...\n", name)
return func() {
fmt.Printf("%s took %v\n", name, time.Since(start))
}
}
func stacked() {
for count := 1; count <= 5; count++ {
defer fmt.Println(count)
}
fmt.Println("the stacked func returns")
}
func single() {
var count int
// defer func() {
// fmt.Println(count)
// }()
defer fmt.Println(count)
count++
// fmt.Println(count)
// the defer runs here
// fmt.Println(count)
}