]> Cypherpunks.ru repositories - gostls13.git/blob - test/defer.go
... changes
[gostls13.git] / test / defer.go
1 // $G $F.go && $L $F.$A && ./$A.out
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package main
8
9 import "fmt"
10
11 var result string
12
13 func addInt(i int) { result += fmt.Sprint(i) }
14
15 func test1helper() {
16         for i := 0; i < 10; i++ {
17                 defer addInt(i)
18         }
19 }
20
21 func test1() {
22         result = ""
23         test1helper()
24         if result != "9876543210" {
25                 fmt.Printf("test1: bad defer result (should be 9876543210): %q\n", result)
26         }
27 }
28
29 func addDotDotDot(v ...interface{}) { result += fmt.Sprint(v...) }
30
31 func test2helper() {
32         for i := 0; i < 10; i++ {
33                 defer addDotDotDot(i)
34         }
35 }
36
37 func test2() {
38         result = ""
39         test2helper()
40         if result != "9876543210" {
41                 fmt.Printf("test2: bad defer result (should be 9876543210): %q\n", result)
42         }
43 }
44
45 func main() {
46         test1()
47         test2()
48 }