Source file
test/defer.go
1
2
3
4
5
6
7
8
9 package main
10
11 import "fmt"
12
13 var result string
14
15 func addInt(i int) { result += fmt.Sprint(i) }
16
17 func test1helper() {
18 for i := 0; i < 10; i++ {
19 defer addInt(i)
20 }
21 }
22
23 func test1() {
24 result = ""
25 test1helper()
26 if result != "9876543210" {
27 fmt.Printf("test1: bad defer result (should be 9876543210): %q\n", result)
28 panic("defer")
29 }
30 }
31
32 func addDotDotDot(v ...interface{}) { result += fmt.Sprint(v...) }
33
34 func test2helper() {
35 for i := 0; i < 10; i++ {
36 defer addDotDotDot(i)
37 }
38 }
39
40 func test2() {
41 result = ""
42 test2helper()
43 if result != "9876543210" {
44 fmt.Printf("test2: bad defer result (should be 9876543210): %q\n", result)
45 panic("defer")
46 }
47 }
48
49 func main() {
50 test1()
51 test2()
52 }
53
View as plain text