New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
recover example #344
recover example #344
Conversation
) | ||
|
||
func main() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There aren't many comments in this sample. Is this intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments in 47eed5e.
Coming from Java's "exceptions world", the recover seems a bit trivial, so I'm not sure what more info should I add.
examples/recover/recover.go
Outdated
recoverFromCustomPanic(-1) | ||
} | ||
|
||
func recoverFromBuiltInPanic(i int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if having two almost identical functions for builtin and custom panics is really useful here.
All things being equal, the sample should be as small as possible. The goal here is to remember (or copy-paste) the proper invocation of recover
inside a defer
, not explore different ways in which Go can panic.
A simple "custom" example should suffice. The panic example has a panic, the recover example should just recover from the same panic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed as you requested in 47eed5e.
Thanks for the feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I didn't use the same panic, as I think it's important to see that the panic might happen in a "lower" function call.
If it's critical I can change to align with the panic example. Or perhaps align the panic example?
Whatever you prefer...
Sorry guys but I'm still stuck with two problems which I hope you know how to fix quickly:
|
IMHO the example should be even simpler, something like (with appropriate comments):
The main idea is not necessarily to teach all the ways in which panic/recover works, but to provide a small copy-pasteable example folks can use if they don't exactly remember the syntax/mechanics. I agree that it's useful to place the potential panic in a function, but the recover can be in But this is just my opinion :-) I'll leave it to @mmcgrana to decide. |
For backlinking, this PR is related to issue #46 |
@kilaka do you intend to keep working on this? |
@kilaka I think your example is in the right direction. I agree the example should be as simple as possible, @eliben's suggestion there seems plausible. I'd also want some brief comments about when/why one uses |
Thanks for discussion. I ended up adding a simplified example (see linked commit). Please open an issue/PR if you have questions or suggestions about the new example. |
No description provided.