Copy func tryDefer () {
defer fmt. Println ( 1 )
fmt. Pintln ( 2 )
panic ( "error" )
}
func main () { //
tryDefer ()
}
Copy package main
import (
"fmt"
"os"
"bufio"
"imooc.com/ccmouse/learngo/lang/functional/fib"
)
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 () {
writeFile ( "fib.txt" )
}
参数在defer语句时计算 / defer列表为后进先出
Copy package main
import (
"fmt"
"os"
"bufio"
"imooc.com/ccmouse/learngo/lang/functional/fib"
)
func tryDefer () {
for i := 0 ; i < 100 ; i ++ {
defer fmt. Println (i)
if i == 30 {
panic ( "printed too many" )
}
}
}
func main () {
tryDefer ()
}
Copy 10
9
8
7
6
5
4
3
2
1
0
panic: printed too many