Home > Backend Development > Golang > Why does this program print 421 in the result?

Why does this program print 421 in the result?

王林
Release: 2024-02-10 19:48:07
forward
946 people have browsed it

Why does this program print 421 in the result?

php editor Xiaoxin will answer a common question for you in this article: "Why does this program print 421 in the result?" This question may involve something in the program some specific logic or errors. We'll help you understand and resolve this issue by analyzing possible causes and solutions. Read on for detailed answers.

Question content

I don’t understand, why does this program print 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())
}
Copy after login

Below I added my guessed comment:

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
}
Copy after login

Workaround

defer It does not call the function, but it < does evaluate its arguments "immediately". Additionally, a call to <确实“立即”评估其论点。另外,对 recover() only stops the panic state when called from a defer function (defer receive() does not meet this condition). See Why doesn't `deferrecover()` catch panics?

Given 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
Copy after login

The execution process of the above code is as follows:

l2: Evaluating the arguments of o(), calling f(), x is incremented to 1 (will be printed later) . o() has not been called yet.

l3: The delayed function has not been called yet, its entire body is temporarily skipped.

l7: f() has not been called yet, x is still 1.

l8: recover() was not called.

l9: Call f(), increment x to 2, and then return, so 2 is passed to panic().

We are in a panic, so now execute the delayed functions (in lifo order).

l8: recover() is called but does not stop the panic state.

l7: f() is now called, increasing x to 3.

l3: This anonymous function is now executed.

l4: recover() returns 2 (the value passed to panic()), which will be printed later, but has not been printed yet because of the The call to o() is deferred. The state of panic ends here.

l5: Call f(), increment x to 4, and print it out immediately.

l4: The delayed function o() is now executed, printing the above value 2.

l2: The delayed function o() is now executed, printing the previously calculated value 1.

The program ends.

The above is the detailed content of Why does this program print 421 in the result?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template