This is a creation in Article, where the information may have evolved or changed.
In the golang of the wrong handling, I here is the summary of their own practice.
1. Log the error messages to the logs
2, panic, and then use the Recover function for exception capture. (I'm just doing a simple example to record this usage.)
function A calls function B, where you can use recover in function A or in function B
Code to save log information to the log ****************************
Write data to the login log
FileName: The file name to save the log to
Content: What to add to the log
Funcwrite_user_info (filename,contentstring) {
File,err: = os. OpenFile (Filename,os. O_append|os. O_rdwr|os. o_create,0666)
Iferr! = Nil {
Log. Println ("Open file failed ...", err)
Return
}
Deferfile. Close ()
Usr_time: = time. Now (). Format ("2006-01-02 15:04:05")
Content = Fmt. Sprint (Usr_time, "", Content, "\ r \ n")
_,err = file. WriteString (content)
Iferr! = Nil {
Log. Println ("Write data to log failed ...", err)
Return
}
}
Using the panic and recover functions ****************************
Functest_painc (T *testing. T) {
Log. Println (GetInt ())
}
Funcgetint () int{
Defer func () int{
Iferr: =recover (); Err! = Nil {
Log. PRINTLN (ERR)
}
Log. Println ("Recover........continue")
Return3
}()
Log. Println ("Begin...........begin")
Panic ("Hello Panic")
}