3 ms·
What is useful about the value of `x` in the following code? x, err := strconv.Atoi("this is invalid") On the contrary, `x` is _lying_ to you about being us
by JamesSwift 1y ago
What is useful about the value of `x` in the following code?
x, err := strconv.Atoi("this is invalid")
On the contrary, `x` is _lying_ to you about being useful and you have absolutely no idea if the string was "0" or "not zero"
- deleted 1y ago[deleted]
- 9rx 1y ago> and you have absolutely no idea if the string was "0" or "not zero" You do – err will tell you. But in practice, how often do you really care? As Go prescribes "Make the zero value useful" your code will be written in such a way that "0" is what you'll end up using downstream anyway, so most of the time it makes no difference. When it does, err is there to use. That might not make sense in other languages, but you must remember that they are other languages that see the world differently. Languages are about more than syntax – they encompass a whole way of thinking about programs.
- kbolino 1y agoI think it's worth noting that, while the general consensus has converged around (T, error) meaning T XOR error, it does not necessarily mean that. There are some places that violate this assumption, like the io.Reader and io.Writer interfaces. Especially io.Reader, where you can have (n>0, io.EOF), which also isn't even a proper error condition! (This isn't a big problem, though, since you rarely need to directly call Read or Write).
- deleted 1y ago[deleted]