logoalt Hacker News

JamesSwiftlast Tuesday at 5:41 PM2 repliesview on HN

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"

Replies

9rxlast Tuesday at 5:48 PM

> 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.

show 1 reply