I can't to understand, why this program prints 421 instead of 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())
}
Below I added the comment how I guess:
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
}
deferdoes not call the function, but it does evaluate its arguments "immediately". Also a call torecover()only stops the panicing state if it gets called from a deferred function (defer recover()does not qualify for this). See Why does `defer recover()` not catch panics?In the light of this: Let's number the lines:
The execution of the above code will go like this:
L2: evaulates the params of
o(),f()is called,xis incremented to1(this will be printed later).o()is not yet called.L3: Deferred function is not called yet, skip its whole body for now.
L7:
f()is not called yet,xremains1.L8:
recover()is not called.L9:
f()is called, incrementsxto2, and returns it, so2is passed topanic().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, incrementsxto3.L3: This anonymous function is now executed.
L4:
recover()returns2(the value that was passed topanic()), this will be printed later, but not yet, as call too()is deferred. Panicing state stops here.L5:
f()is called, incrementsxto4, it gets printed right away.L4: deferred function
o()is now executed, printing the above value2.L2: deferred function
o()is now executed, printing the previously evaluated value1.End of program.