5 ms·
Does Go really aim to discourage the programmer from adding runtime checks? What's the reasoning there?
by MaxBarraclough 6y ago
Does Go really aim to discourage the programmer from adding runtime checks? What's the reasoning there?
- orisho 6y agoAn assertion, I think, would crash the program. In that sense implementing them is simple - just panic when the condition is violated. Go prefers plain checks that return errors, though. Panics should be used for truly unexpected conditions, such as a nil pointer. Invalid input, as in this case, would be better served as an error.
- randomdata 6y ago> Panics should be used for truly unexpected conditions, such as a nil pointer. Exceptional cases, if you will. I find it funny when people say Go doesn't have exceptions when Go is the only mainstream language that advocates for and prefers the use of exceptions.
- dmurray 6y agoTo encourage "proper error handling and reporting" according to the FAQ. https://golang.org/doc/faq#assertions https://golang.org/doc/faq#assertions
- sanxiyn 6y agoYes, Go really does discourage assertions.