blob: bef8fbe26a6cd7a187269933acdfd84050296a28 [file] [log] [blame]
Rob Pikec8476472009-01-27 15:08:08 -08001// $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
7package main
8
9import "fmt"
10
11var result string
12
Rob Piked2fc5d62010-02-02 10:53:37 +110013func addInt(i int) { result += fmt.Sprint(i) }
Rob Pikec8476472009-01-27 15:08:08 -080014
15func test1helper() {
16 for i := 0; i < 10; i++ {
17 defer addInt(i)
18 }
19}
20
21func test1() {
Rob Piked2fc5d62010-02-02 10:53:37 +110022 result = ""
23 test1helper()
Rob Pikec8476472009-01-27 15:08:08 -080024 if result != "9876543210" {
Rob Piked2fc5d62010-02-02 10:53:37 +110025 fmt.Printf("test1: bad defer result (should be 9876543210): %q\n", result)
Rob Pikec8476472009-01-27 15:08:08 -080026 }
27}
28
Russ Cox2ee420f2010-09-24 11:55:48 -040029func addDotDotDot(v ...interface{}) { result += fmt.Sprint(v...) }
Rob Pikec8476472009-01-27 15:08:08 -080030
31func test2helper() {
32 for i := 0; i < 10; i++ {
33 defer addDotDotDot(i)
34 }
35}
36
37func test2() {
Rob Piked2fc5d62010-02-02 10:53:37 +110038 result = ""
39 test2helper()
Rob Pikec8476472009-01-27 15:08:08 -080040 if result != "9876543210" {
Rob Piked2fc5d62010-02-02 10:53:37 +110041 fmt.Printf("test2: bad defer result (should be 9876543210): %q\n", result)
Rob Pikec8476472009-01-27 15:08:08 -080042 }
43}
44
45func main() {
Rob Piked2fc5d62010-02-02 10:53:37 +110046 test1()
47 test2()
Rob Pikec8476472009-01-27 15:08:08 -080048}