|
|
|
|
|
by natefinch
2629 days ago
|
|
I've been writing Go for over 6 years. Error handling at the source of failure is a feature, not a bug. Rob Pike's example code is bad and I would not allow it through a code review. Why? It makes the code lie. It appears as though the writes cannot fail, but in fact they can, and later writes become no-ops. On top of that, it means that you lose the context of which write failed. Did you fail writing the header, the body, or the footer? You can't know anymore. This is like try {
doThing()
doThing2()
doThing3()
catch (Exception ex) {
// handle
}
You can't tell what failed. You can't tell what is possible to fail. The handler can't differentiate. |
|
>Did you fail writing the header, the body, or the footer? You can't know anymore.
You are writing to the same stream in both cases. The error wouldn't be caused by the header or footer data, but by some property of the stream, like it being closed, or the disk running out of space. Knowing whether the failure occurred while you were writing to the header or footer is unnecessary information, and therefore obfuscatory.