1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // This file contains the code snippets included in "Defer, Panic, and Recover." 6 7 package main 8 9 import ( 10 "fmt" 11 "io" 12 "os" 13 ) 14 15 func a() { 16 i := 0 17 defer fmt.Println(i) 18 i++ 19 return 20 } 21 22 // STOP OMIT 23 24 func b() { 25 for i := 0; i < 4; i++ { 26 defer fmt.Print(i) 27 } 28 } 29 30 // STOP OMIT 31 32 func c() (i int) { 33 defer func() { i++ }() 34 return 1 35 } 36 37 // STOP OMIT 38 39 // Initial version. 40 func CopyFile(dstName, srcName string) (written int64, err error) { 41 src, err := os.Open(srcName) 42 if err != nil { 43 return 44 } 45 46 dst, err := os.Create(dstName) 47 if err != nil { 48 return 49 } 50 51 written, err = io.Copy(dst, src) 52 dst.Close() 53 src.Close() 54 return 55 } 56 57 // STOP OMIT 58 59 func main() { 60 a() 61 b() 62 fmt.Println() 63 fmt.Println(c()) 64 } 65