Go: 7-1 资源管理与出错处理

defer的调用
 
 ##### Error处理
 

package main

import ( “fmt” “os”

“bufio”

“imooc/function/fib” )

func tryDefer() { for i := 0; i < 100; i++ { defer fmt.Println(i) if i == 30 { // Uncomment panic to see // how it works with defer // panic(“printed too many”) } } }

func writeFile(filename string) { file, err := os.OpenFile(filename, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0666) //错误处理 if err != nil { if pathError, ok := err.(*os.PathError); !ok { panic(err) } else { fmt.Printf(“%s, %s, %s\n”, pathError.Op, pathError.Path, pathError.Err) } return } defer file.Close()

writer := bufio.NewWriter(file) defer writer.Flush()

f := fib.Fibonacci() for i := 0; i < 20; i++ { fmt.Fprintln(writer, f()) } }

func main() { tryDefer() writeFile(“fib1.txt”) } ```