3 ms·
Errors are just return value, but in go, you either explicitly ignore the error returned (using the "blank identifier" _ ), or, you assign it, in which case you
by gouggoug 12y ago
Errors are just return value, but in go, you either explicitly ignore the error returned (using the "blank identifier" _ ), or, you assign it, in which case you have to deal with it (otherwise go will complain about an unused variable).
An that is what makes go awesome. In java or php, you, as a developer, can never know wether the functions you are calling will throw an exception. The only way to know? Read the doc, if you're lucky and there's a doc, or read the code... The result? your program will crash if you didn't add your try..catch block.
Go forces you to either explicitly ignore errors (and your fellow co-worker will know you did it on purpose) or deal with them. No surprises.
- Peaker 12y agoI get the same in C, with gcc's __attribute__((warn_unused_result)).
- aktau 12y agoThis comment should be more prominent, warn_unused_result is pretty awesome!
- tmhedberg 12y ago> In java or php, you, as a developer, can never know wether the functions you are calling will throw an exception. That's what checked exceptions are for in Java.
- gouggoug 12y agoThe larger problem with exceptions, checked or not, is that they interrupt the flow of your program, which is too bad since most exceptions are not that exceptional. That problem is exacerbated by developers who don't know how and when to throw exceptions and end up throwing exceptions for easily recoverable errors. The other thing I dislike about exception is that your code ends up with tons of try..catch blocks.