Exceptions
One of the great things about Go is that the standard patterns are that you should always handle errors when they occur instead of bubbling them up to the top and presenting them to the user. Having said that, there is always a case when the unexpected happens. The secret to this is to know about it and to fix the problem when it occurs. There are many exception logging platforms on the market. However, the two techniques we have discussed are, in my opinion, more than sufficient for tracing the few errors that we hopefully will find in our web application.
Panic and recover
Golang has two great methods for handling unexpected errors:
- Panic
- Recover
Panic
The built-in panic function stops the normal execution of the current goroutine. All the deferred functions are run in the normal way then the program is terminated:
func panic(v interface{})
Recover
The recover function allows an application to manage the behavior of a panicking goroutine. When called inside a deferred function, recover...