golang的defer关键字执行的时机

  在golang中,我们使用defer语句来进行一些错误处理和收尾工作,它的作用类似java里面finally关键字的作用。不过不论是java的finally关键字还是c++的Raii类,我们都能清楚地知道它们的作用域和执行的时机,那么golang中defer关键字所处理的内容是在什么时候执行的呢?http://studygolang.com/articles/2593这篇文章使我豁然开朗。

  首先官方文档里面说了:“A "defer" statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.”意思就是函数返回、函数结束或者对应的goroutine发生panic的时候defer就会执行。  

  然后需要注意的是因为golang支持多值返回,所以是在返回前把返回值压到栈中的,而c语言是把返回值存到寄存器中返回。

  golang返回时,先把返回值压栈;然后执行defer函数,如果defer函数中有修改栈中的返回值(不过不应该这样做),那么返回值会被修改;之后进行跳转返回。

  这样的话我们就清楚了defer执行的时机了,即使你把defer写在返回之前很远的地方,它还是会在函数返回之前执行。

  如果一个作用域中有多个defer,那么返回前的执行顺序是先执行后调用的defer,在执行早调用的defer。

  官方文档里面有几个例子可以很好地说明defer的作用时机:

 1 lock(l)
 2 defer unlock(l)  // unlocking happens before surrounding function returns
 3 
 4 // prints 3 2 1 0 before surrounding function returns
 5 for i := 0; i <= 3; i++ {
 6     defer fmt.Print(i)
 7 }
 8 
 9 // f returns 1
10 func f() (result int) {
11     defer func() {
12         result++
13     }()
14     return 0
15 }

 

posted @ 2017-05-23 17:06  stormpeach  阅读(2852)  评论(0编辑  收藏  举报