Recover
It is a built-in function in Go language that allows you to enter the downtime processgoroutine
Recover only works in the delay function defer. During normal execution, calling recover will return nil and have no other effect, if the current one isgoroutine
In a panic, calling recover can capture the input value of panic and resume normal execution.
Generally speaking, no processing should be done to programs that enter panic downtime, but sometimes, we need to recover from the downtime. At least we can do some operations before the program crashes. For example, when the web server encounters unpredictable serious problems, all connections should be closed before the crash. If nothing is done, the client will be in a waiting state. If the web server is still in the development stage, the server can even feed the exception information to the client to help debug.
In other languages, downtime often exists in the form of exceptions, the underlying exception is thrown, and the upper logic passestry/catch
The mechanism catches exceptions. Serious exceptions that are not caught will cause downtime. The caught exceptions can be ignored and the code will continue to run.
Go language does not have an exception system, and it uses panic to trigger a downtime similar to throwing an exception in other languages.recover
The downtime recovery mechanism corresponds to the try/catch machine in other languages
How to use Recover:
Defer can read the name return value
theory:If you do not recover, the entire program will hang up.
The usage of Recover() is:WillRecover()
Write it in the defer, and before panic occurs, call the defer thing first (let the system method domain end, there is code to be executed.) When the program encounters panic (of course, it can also call abnormal situations normally), the system will skip the subsequent code and enter the defer if the defer function is inrecover()
, then returns the captured panic value.
Summarize:When using recover() to catch panic exceptions, defer is needed to read an anonymous function.
defer func(){ If err:=recover();err!=nil{//Be careful to make a judgmentPrint capturederrcontent } }()// Used to call this anonymous function package main import ( “fmt” “log” ) func tast2() { (“sssssssss”) } func tast(x int) { defer func() { if err:=recover();err !=nil{ (err) } }() var a [10]int a[x]=1222 (a) } func main(){ tast(20) tast2() }
hopegoroutine
The remaining program will not run due to crashes when the function is executed, and a default return value is set.
package main import ( "fmt" "time" ) func calcRem(i int) (res int) { defer func() { if err := recover(); err != nil { ("error: %s\n", err) res = 999 //Interference output } }() res = 10 / (10 % i) //When the remainder is taken as 0, res is 0 ("10 / (10 %% %d) = %d\n", i, res) return } func main() { resCH := make(chan int, 10) defer close(resCH) for i := 1; i <= 10; i++ { go func(i int) { res := calcRem(i) resCH <- res }(i) ( * 100) } () ("ch len is: %d\n", len(resCH)) for i := 1; i <= 10; i++ { res := <-resCH (res) } }
output
error: runtime error: integer divide by zero
error: runtime error: integer divide by zero
10 / (10 % 3) = 10
10 / (10 % 4) = 5
error: runtime error: integer divide by zero
10 / (10 % 6) = 2
10 / (10 % 7) = 3
10 / (10 % 8) = 5
10 / (10 % 9) = 10
error: runtime error: integer divide by zero
ch len is: 10
999
999
10
5
999
2
3
5
10
999
This is the end of this article about how to use Guam and golang recover(). For more information about the usage of golang recover(), please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!