It can be useful to print the error message returned by panic (if one is returned). That should be simple enough. Here were are not concerned that panic can return any, just to keep it simple.
|
|
But above example reports error to be nil
. Which is not true.
The ‘problem’ is in the defer
statement, actually. Defer behavior, as described in the Go Blog post:
- A deferred function’s arguments are evaluated when the defer statement is evaluated.
- Deferred function calls are executed in Last In First Out order after the surrounding function returns.
- Deferred functions may read and assign to the returning function’s named return values.
In our case, we have to use a named return value, to actually return err value from the function:
|
|
Even in panic, one needs to keep a good posture ;).
References: