This is a creation in Article, where the information may have evolved or changed.
Defer,recover:
Package Mainimport ("FMT" " Time") func test () {defer func () {//use recover to catch exceptions in defer//defer at the end of the function execution, execute the method ifERR: = Recover (); Err! =Nil {fmt. PRINTLN (Err)}} () B:=0A:= -/FMT B. Println (a)return}func Main () { for{test () time. Sleep (time. Second)}varA []inta= Append (A,Ten, -,383) A=Append (A, a ...) Fmt. Println (a)}
Panic
Package Mainimport ("Errors" "FMT" " Time") func initconfig () (err error) {returnErrors. New ("Init config failed")}func Test () {//defer func () {// //use recover to catch exceptions in defer// //defer at the end of the function execution, execute the method//If err: = Recover (); Err! = Nil {//FMT. PRINTLN (ERR)// } //}()ERR: =initconfig ()ifErr! =Nil {panic (err)}return}func Main () { for{test () time. Sleep (time. Second)}varA []inta= Append (A,Ten, -,383) A=Append (A, a ...) Fmt. Println (a)}