5 ms·
Can anyone explain this to me?
by Sebb767 5y ago
Can anyone explain this to me?
- svieira 5y agoLooks 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!
- Sebb767 5y agoAh, I missed that line. Now I feel stupid ;)
- Cthulhu_ 5y agoThey're defining a const named true and false to their inverse values, shadowing the builtin true/false keywords.
- carl_dr 5y ago!!!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.
- JoeCortopassi 5y ago`!` 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