3 ms·
Here's a short example comparing existing error handling, the check proposal, and the experimental try proposal https://gist.github.com/kyleconroy/e48c83425349
by conroy 7y ago
Here's a short example comparing existing error handling, the check proposal, and the experimental try proposal
https://gist.github.com/kyleconroy/e48c83425349f2d3954d4e76471ff3f5 https://gist.github.com/kyleconroy/e48c83425349f2d3954d4e764...
- apta 7y agoFunny how it's also wrong. handle err { w.Close() os.Remove(dst) // (only if a check fails) } the error returned from w.Close() is not handled.
- zeeboo 7y agoThat `Close` call only happens in the case that an error happened during either copying or closing. The point of checking errors on `Close` is so that you catch problems during flushing of any buffered writes. In this case, the `Close` is not to ensure the data is flushed, but to ensure that the file descriptor is released in error scenarios, justifying that the error need not be checked. On the other hand, w := try(s.Create(dst)) try(io.Copy(w, r)) try(w.Close()) return nil this code has the problem where if there were an error in the `Copy`, the file descriptor would be leaked. So, yes, one of the examples is wrong.
- paulkon 7y agoLOC reduction of 10% with check/handle statements and 20% with try()/defer along with somewhat reduced clarity of what will happen on error. I'm not a fan of the verbosity at present either, but seems like the check/handle syntax is less direct and the try()/defer func syntax is less flexible.
- kodablah 7y agoWhy is there a defer in the "try" example? To just show wrapping even though the "existing" one doesn't wrap? Also, the "try" example doesn't defer-close "w" in case copy fails. Seems that they aren't the same example.
- ixwt 7y agotry() implicitly returns when there's an error, and sets the error in the case of an error. Then the defer statement would handle the error, as the return is happening.
- kodablah 7y agoI'm saying it doesn't match what "existing" is doing which doesn't wrap the error. If you want comparable examples, they need to be doing the same thing, not one wrapping w/ an Errorf and another not.
- lerno 7y agoFunny, I thought I would like the ”try”, but looking at these examples I prefer the Go1 version.