1// run 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// Test defer. 8 9package main 10 11import "fmt" 12 13var result string 14 15func addInt(i int) { result += fmt.Sprint(i) } 16 17func test1helper() { 18 for i := 0; i < 10; i++ { 19 defer addInt(i) 20 } 21} 22 23func 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 32func addDotDotDot(v ...interface{}) { result += fmt.Sprint(v...) } 33 34func test2helper() { 35 for i := 0; i < 10; i++ { 36 defer addDotDotDot(i) 37 } 38} 39 40func 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 49func main() { 50 test1() 51 test2() 52} 53