Search code examples
gopanicdefer-keyword

Why this program prints 421 in result?


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
}

Solution

  • defer does not call the function, but it does evaluate its arguments "immediately". Also a call to recover() 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:

    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
    

    The execution of the above code will go like this:

    L2: evaulates the params of o(), f() is called, x is incremented to 1 (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, x remains 1.

    L8: recover() is not called.

    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.