“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Say goodbye and never break down

outage

Saying it was a sunny morning, got the take-away little elder brother sent me the most beloved malatang, I opened the because yesterday just filling the three hair money electricity and restore the use of computer, boarded the is full of beauty friends WeChat, immediately familiar voice echoed in my this three hundred – square – meter in the bedroom, I this damn charm, it opens at the glittering beauty face, A line of text greeted, the project out of the bug, directly down, five minutes to deal with, or malatang to you down… It’s nice to beDigress… Without further ado, load up

What is down

I think downtime in go is similar to reporting an error,Array access out of bounds, null pointer references, and other runtime errors can cause outages, the program will stop after the outage,The compiler outputs the corresponding error message, including panic Value, which is usually some kind of error message, and the stack trace of the function callDowntime may seem like a bad idea, but sometimes a reasonable downtime is a wise way to cut your losses.

Manually trigger the downtime

Like other languages, such as Java, which manually throws an error, Go also has a way to manually trigger outages — panic ()

package main

import "fmt"
func main(a) {
    //xxxxxxx
    fmt.Println("I have 10,000 lines of code on it.") 
    panic("Ten thousand lines won't help. Something's wrong with me.")
    fmt.Println("I'm not working.")}Copy the code

The Go program outputs stack and Goroutine information to the console during an outage, so it’s also easy to know where the error occurred. This shows the error at line 7

If we want the method to end up crashing without causing other statements to execute, we can use defer

package main

import "fmt"
func main(a) {

    //xxxxxxx
    fmt.Println("I have 10,000 lines of code on it.") 
    defer panic("Ten thousand lines won't help. Something's wrong with me.")
    fmt.Println("Hey, I'm working again.")}Copy the code

Why are there two error messages? Because defer loads on the current line, the first error is generated, and the second error is generated when it executes after the method.

Outage recovery

If we want to continue executing after an outage, we can use recover, which works in much the same way as try/catch in Java. Recover only works in the deferred function defer. During normal execution, calling recover returns nil and has no other effect. If the current Goroutine is in a panic, the call to Recover captures the input values of Panic and resumes normal execution.

package main

import "fmt"

func test (a) {
    defer func(a) {
        err := recover(a)iferr ! =nil {
            fmt.Println("Believe it or not, I said yes.")}} ()// Call an anonymous function
    demo ()
}

func demo (a)  {
    fmt.Println("Ten thousand lines won't work. Something's wrong with me. Nothing works down there.") 
    defer panic("What can I say? I can't say anything anymore.")}func main(a) {

    //xxxxxxx
    fmt.Println("I have 10,000 lines of code on it.") 
    test()
    fmt.Println("Hey, I'm working again.")}Copy the code

The combination of Panic and Recover has the following features:Panic did not recover, the program is down. There is panic and Recover, and the program will not break down. After executing the corresponding defer, the program will continue to execute after exiting the current function from the breakdown point.

ps

While Panic/Recover can emulate exceptions in other languages, it is not recommended to use this feature frequently when writing normal functions. In the panic-triggered defer function, you can continue to call panic to further throw out errors, If you want to set the return value of the current function when catching an error, you can set the return value directly using named return value

You thought it was over

Figure out how to recover the outage I holdrecoverDirectly will lead to the breakdown of the method to capture, and then go up is a crow by plane, this time completed, quickly reply processing is not finished will receive a reply, write what play, you so deal with it, leave the pit to fill, not only this month malatang did not, next month also did not really a beautiful day…After reading, you find any mistakes, write them down below! Rub it in with my Black Tiger Fu!