defer
Keyword
Defer is used to ensure that a function call is performed later in a program’s execution, usually for purposes of cleanup. defer
is often used where e.g. ensure
and finally
would be used in other languages.
The canonical examples are unlocking a mutex or closing a file.
Demo
Demo1
package main
import (
"fmt"
"os"
)
func main() {
f := createFile("/tmp/defer.txt")
defer closeFile(f)
writeFile(f)
}
func createFile(p string) *os.File {
fmt.Println("creating")
f, err := os.Create(p)
if err != nil {
panic(err)
}
return f
}
func writeFile(f *os.File) {
fmt.Println("writing")
fmt.Fprintln(f, "data")
}
func closeFile(f *os.File) {
fmt.Println("closing")
err := f.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
# Run
$ go run defer.go
creating
writing
closing
Demo2
package main
import (
"fmt"
)
func main() {
for i := 0; i < 5; i++ {
defer fmt.Printf("%d ", i)
}
}
//Ouput:
// 4 3 2 1 0
We can do better by exploiting the fact that arguments to deferred functions are evaluated when the defer
executes. The tracing routine can set up the argument to the untracing routine. This example:
func trace(s string) string {
fmt.Println("entering:", s)
return s
}
func un(s string) {
fmt.Println("leaving:", s)
}
func a() {
defer un(trace("a"))
fmt.Println("in a")
}
func b() {
defer un(trace("b"))
fmt.Println("in b")
a()
}
func main() {
b()
}
prints
entering: b
in b
entering: a
in a
leaving: a
leaving: b
For programmers accustomed to block-level resource management from other languages, defer
may seem peculiar, but its most interesting and powerful applications come precisely from the fact that it’s not block-based but function-based.
Demo3
func f() (result int) {
defer func() {
result++
}()
return 0
}
要使用defer时不踩坑,最重要的一点就是要明白,return xxx这一条语句并不是一条原子指令!
函数返回的过程是这样的:先给返回值赋值,然后调用defer表达式,最后才是返回到调用函数中。
defer表达式可能会在设置函数返回值之后,在返回到调用函数之前,修改返回值,使最终的函数返回值与你想象的不一致。
其实使用defer时,用一个简单的转换规则改写一下,就不会迷糊了。改写规则是将return语句拆成两句写,return xxx会被改写成:
返回值 = xxx -> result = 0
调用defer函数
return 对应的变量 -> return result
先看例1,它可以改写成这样:
func f() (result int) {
result = 0 //return语句不是一条原子调用,return xxx其实是赋值+ret指令
func() { //defer被插入到return之前执行,也就是赋返回值和ret指令之间
result++
}()
return
}
所以这个返回值是1。