Catching panics for long running processes
When implementing long running processes, it's possible that certain code paths will result in a panic. This is usually common for things uninitialized maps and pointers, as well as division by zero problems in the case of poorly validated user input.
Having a program crash completely in these cases is frequently much worse than the panic itself, and so it can be helpful to catch and handle panics.
Getting ready
Refer to the Getting ready section of the Handling errors and the Error interface recipe in this chapter.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create the
chapter4/panic
directory and navigate to it. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter4/panic or use this as an exercise to write some of your own code.
- Create a file called
panic.go
with the following content:
package panic import ( "fmt" "strconv...