github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/doc/progs/defer.go (about) 1 // cmpout 2 3 // Copyright 2011 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 // This file contains the code snippets included in "Defer, Panic, and Recover." 8 9 package main 10 11 import ( 12 "fmt" 13 "io" 14 "os" 15 ) 16 17 func a() { 18 i := 0 19 defer fmt.Println(i) 20 i++ 21 return 22 } 23 24 // STOP OMIT 25 26 func b() { 27 for i := 0; i < 4; i++ { 28 defer fmt.Print(i) 29 } 30 } 31 32 // STOP OMIT 33 34 func c() (i int) { 35 defer func() { i++ }() 36 return 1 37 } 38 39 // STOP OMIT 40 41 // Initial version. 42 func CopyFile(dstName, srcName string) (written int64, err error) { 43 src, err := os.Open(srcName) 44 if err != nil { 45 return 46 } 47 48 dst, err := os.Create(dstName) 49 if err != nil { 50 return 51 } 52 53 written, err = io.Copy(dst, src) 54 dst.Close() 55 src.Close() 56 return 57 } 58 59 // STOP OMIT 60 61 func main() { 62 a() 63 b() 64 fmt.Println() 65 fmt.Println(c()) 66 }