Cup*_*ate -1 go panic defer-keyword
我不明白,为什么这个程序打印421而不是431?
package main
import "fmt"
var x int
func f() int {
x++
return x
}
func main() {
o := fmt.Println
defer o(f())
defer func() {
defer o(recover())
o(f())
}()
defer f()
defer recover()
panic(f())
}
Run Code Online (Sandbox Code Playgroud)
下面我添加了我猜测的评论:
package main
import "fmt"
var x int
func f() int {
x++
return x
}
func main() {
o := fmt.Println
defer o(f()) // x=1
defer func() {
defer o(recover()) // x=3 from panic (but if we ll execute this program we ll see 2)
o(f()) // x=4
}()
defer f() // x=2
defer recover() //nil
panic(f()) // x=3
}
Run Code Online (Sandbox Code Playgroud)
defer不调用该函数,但它确实“立即”评估其参数。此外,recover()如果从延迟函数调用,则仅停止紧急状态(defer recover()不符合此条件)。请参阅为什么“deferrecover()”不会捕获恐慌?
鉴于此:让我们对行进行编号:
L1: o := fmt.Println
L2: defer o(f()) // x = 1
L3: defer func() {
L4: defer o(recover()) // recover() returns 2
L5: o(f()) // x = 4, it gets printed
L6: }()
L7: defer f() // after panic: x = 3
L8: defer recover()
L9: panic(f()) // x = 2
Run Code Online (Sandbox Code Playgroud)
上面代码的执行过程如下:
L2:评估 的参数o(),f()被调用,x递增到1(这将在稍后打印)。o()尚未被调用。
L3:延迟函数还没有被调用,暂时跳过它的整个函数体。
L7:f()尚未调用,x仍为1。
L8:recover()不叫。
L9: f() is called, increments x to 2, and returns it, so 2 is passed to panic().
We're in a panicking state, so deferred functions are executed now (in LIFO order).
L8: recover() is called but does not stop the panicing state.
L7: f() is called now, increments x to 3.
L3: This anonymous function is now executed.
L4: recover() returns 2 (the value that was passed to panic()), this will be printed later, but not yet, as call to o() is deferred. Panicing state stops here.
L5: f() is called, increments x to 4, it gets printed right away.
L4: deferred function o() is now executed, printing the above value 2.
L2: deferred function o() is now executed, printing the previously evaluated value 1.
End of program.