]> Cypherpunks.ru repositories - gostls13.git/blob - test/defer.go
test for defer
[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) {
14         result += fmt.Sprint(i)
15 }
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         }
29 }
30
31 func addDotDotDot(v ...) {
32         result += fmt.Sprint(v)
33 }
34
35 func test2helper() {
36         for i := 0; i < 10; i++ {
37                 defer addDotDotDot(i)
38         }
39 }
40
41 func test2() {
42         result = "";
43         test2helper();
44         if result != "9876543210" {
45                 fmt.Printf("test2: bad defer result (should be 9876543210): %q\n", result);
46         }
47 }
48
49 func main() {
50         test1();
51         test2();
52 }