Logging fatal errors
Using the log
package, we can also log fatal errors. The Fatal()
, Fatalf()
, and Fatalln()
functions are similar to Print()
, Printf()
, and Println()
. The difference is after logging, Fatal()
functions are followed by an os.Exit(1)
system call. The log
package also has the following functions: Panic
, Panicf
, and Panicln
. The difference between the Panic
functions and the Fatal
functions is that the Panic
functions are recoverable. When using the Panic
functions, you can use the defer()
function, whereas when using the Fatal
functions, you cannot. As stated earlier, the Fatal
functions call os.Exit()
; a defer
function will not be called when os.Exit()
gets called. There may be some instances where you want to abort the program immediately with no possibility of recovery. For example, the application may have gotten to a state where it is best to exit it before data corruption or undesired behavior results. Or, you may have developed a command-line utility that is...