2012-02-16 21:48:57 -07:00
|
|
|
// run
|
2009-01-27 16:08:08 -07:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-18 20:28:53 -07:00
|
|
|
// Test defer.
|
|
|
|
|
2009-01-27 16:08:08 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
var result string
|
|
|
|
|
2010-02-01 16:53:37 -07:00
|
|
|
func addInt(i int) { result += fmt.Sprint(i) }
|
2009-01-27 16:08:08 -07:00
|
|
|
|
|
|
|
func test1helper() {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
defer addInt(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func test1() {
|
2010-02-01 16:53:37 -07:00
|
|
|
result = ""
|
|
|
|
test1helper()
|
2009-01-27 16:08:08 -07:00
|
|
|
if result != "9876543210" {
|
2010-02-01 16:53:37 -07:00
|
|
|
fmt.Printf("test1: bad defer result (should be 9876543210): %q\n", result)
|
2013-02-12 11:17:49 -07:00
|
|
|
panic("defer")
|
2009-01-27 16:08:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-24 09:55:48 -06:00
|
|
|
func addDotDotDot(v ...interface{}) { result += fmt.Sprint(v...) }
|
2009-01-27 16:08:08 -07:00
|
|
|
|
|
|
|
func test2helper() {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
defer addDotDotDot(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func test2() {
|
2010-02-01 16:53:37 -07:00
|
|
|
result = ""
|
|
|
|
test2helper()
|
2009-01-27 16:08:08 -07:00
|
|
|
if result != "9876543210" {
|
2010-02-01 16:53:37 -07:00
|
|
|
fmt.Printf("test2: bad defer result (should be 9876543210): %q\n", result)
|
2013-02-12 11:17:49 -07:00
|
|
|
panic("defer")
|
2009-01-27 16:08:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2010-02-01 16:53:37 -07:00
|
|
|
test1()
|
|
|
|
test2()
|
2009-01-27 16:08:08 -07:00
|
|
|
}
|