Golang Defer Panic && Recover

Error handling mechanism for the Go language, provides three key defer, panic and recover. With the traditional try-catchdifferent statements appeared defer does not require layers were try-catchnested.

Defer action statement is to ensure that the last sentence in the defer execution, even if the program runs error, defer or after the statement is executed and the program ends. After the cleaning work is similar to the program finishes running.

defer registration statement can be understood as a few custom clean-up function, if a function is executed defer statement, then the statement is wrapped defer function will be registered, and then perform in the final.

defer the use of a few places to note the statement

  • defer statement wrapped function, its parameters must be evaluated when the defer statement is executed. And when using the defer statement parcel, it will be called.
func a() {
    i := 0
    defer fmt.Println(i)
    i ++
    return 
}

At this time, when the operation defer, i of the evaluated as 0, passing Print function.

  • If there are multiple defer statements include functions, then these functions comply with the principles of FILO, followed by the implementation.
  • defer function reads the value returned by the function, operation, and even modify the return value
func c() (i int) {
    defer func() { i++ }()
    return 1
}

This time, defer to directly modify the original return value, it returns a value of 2.

Panic-recoverStatement
Panic-recoverstatement is the Go language proposed for error exception handling keywords.
Panic statement can let the program fall into a state of panic, the program will stop the original workflow, and then press the order execution defer've statement, and then returns a function call, the caller concerned, at the moment, into a state of panic. Then before the action continues until all this time goroutine in return, and the program crash out. Panic state of the entire program may have caused a number of reasons, out of bounds for the array or nil operations will result in panic state of the program, of course, we show call panic () function will cause the program to enter the panic state.

Recover statement is used to program the state of panic were statements recovery. recover its function only when the calling to be effective in the defer, defer if called outside, is invalid. If the program does not enter a state of panic, then recover the function returns nil, on the contrary, recover function returns when calling panic function. Then let the program back to normal.

Here's an example of a complete understanding of example, we will understand the usage defer, panic, recover the.

package main

import "fmt"

func main() {
    f()
    fmt.Println("Returned normally from f.")
}

func f() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered in f", r)
        }
    }()
    fmt.Println("Calling g.")
    g(0)
    fmt.Println("Returned normally from g.")
}

func g(i int) {
    if i > 3 {
        fmt.Println("Panicking!")
        panic(fmt.Sprintf("%v", i))
    }
    defer fmt.Println("Defer in g", i)
    fmt.Println("Printing in g", i)
    g(i + 1)
}

Go routine and panic
Go language is built multiprotocol process language, multiple coroutines, there have been Panic, how to handle it.
Go language, time defer the call, the call will goroutine coroutine calls and functions defer associate. defer function call corresponding to a respective state coroutine. So, if you encounter a time span coroutine, cross coroutine defer recover will fail, that is no interference between the two coroutines. Goroutine in a panic, it does not call the delay function of other goroutine.
But a goroutine running panic, if not handled well, will also affect the operation of other goroutine.

So, for more than coroutine, they should deal with their respective goroutine panic situation.

Guess you like

Origin www.cnblogs.com/wAther/p/12285715.html