Exceptions
Panic
Panic is a built-in function that stops the normal execution flow. When you call panic in your code, it means you’ve decided that your caller can’t solve the problem. Therefore, you should use panic only in rare cases where it’s not safe for your code or anyone integrating your code to continue at that point. The code sample below demonstrates how panic works:
As shown above, when panic is used and not handled, the execution flow stops, all deferred functions are executed in reverse order, and stack traces are printed.
Recover
To report an error as a return value, you have to call the recover function in the same goroutine as where the panic function is called, retrieve an error struct from the recover function, and pass it to a variable:
Every deferred function will be executed after a function call but before a return statement. Therefore, you can set a returned variable before a return statement is executed.
Next example: Methods