Hacker News new | past | comments | ask | show | jobs | submit login




Great point, I'll edit my comment to say "(or its equivalent)", instead of "(that precise sequence of characters, no matter what you've redefined true and false to be)". Not sure why I originally wrote it that way.


One might want to add that it does work when you use a variable however. https://play.golang.org/p/6UfNFWm_-JR


The issue isn't that they're constants, but that GP called them "true" and "false" and assigned the opposite values you'd expect. You can break it in exactly the same way with variables: https://play.golang.org/p/EVU84l0A57I

Kinda crazy to me that Go doesn't reserve the words "true" and "false", but ¯\_(ツ)_/¯


Can anyone explain this to me?


Looks like the `true` and `false` _bindings_ in Go are mutable and can be re-assigned. The same thing was possible in Python 2, IIRC:

    False, True = True, False # Have fun debugging!


Ah, I missed that line. Now I feel stupid ;)


!!!true is equal to false in JS, and in Go (and in any other language I can think of.)

In that Go example above, the author is reassigning true and false to be their opposites.


`!` is added to values in most languages as a shorthand for saying "give me the opposite boolean value of this. So `!true` would equal `false`

Some people add two exclamation points as a shorthand to cast a value to a boolean. So if you wanted to see if something was 'truthy', you could say `!!truthyValue` and it would return ` true` instead of the value itself. Literally what your asking the language is "give me the opposite boolean value of the opposite boolean value of `truthyValue`"

Now you can probably see why three exclamation points is silly, it's not giving you anything that a single exclamation point wouldn't give you. Both `!truthyValue` and `!!!truthyValue` evaluate to false, you are literally saying "give me the opposite boolean value of the opposite boolean value of the opposite boolean value of `truthyValue`"

The example in Go is intentionally misleading because Go lets you reassign the values for `true` and `false`. It's going through all the same steps I described above, but it's starting with a value opposite of what you think it is


They're defining a const named true and false to their inverse values, shadowing the builtin true/false keywords.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: