> The rationale is pretty simple: Only identifiers that must be keywords for syntactic reasons are keywords.
> In fact, in the very beginning there was some discussion as to whether things like nil, iota, etc. should be keywords. Eventually we agreed on the rule above which settled it.
https://github.com/golang/go/issues/18193#issuecomment-26492...
On the other hand, why would I ever want to do that? It's not something one might do by accident. It's along the line of C letting you say
idx[arr]
instead of arr[idx]
It's unfortunate that it is possible, but there is no good reason to ever do this unless one is trying to confuse one's enemies. old := x
new := foo(&x)
if old != new {
return &Bar{} // oops, return new(Bar) won't compile
}
That should at least be caught by linters.As you point out, accidentally redefining the meaning of len or new or close is far more likely.
OTOH, it's very unlikely you type accidentally `true := x` in your code, and even if you did, that would very probably be caught by the compiler anyway.
Anybody who is still human and can do a typo.
You don't need to have a variable like "tue" to make such a typo. Since your writing an if statement, you already have "true" and "false" in mind, so you just need to be a little absentminded and voila, instead of foo := false you've written true := false. Plus, lots of naive editors will offer to autocomplete something starting with t to true (if they find the token true used elsewhere within the file).
>the compiler will tell you "true declared and not used".
Only if you don't use it. But a few lines below you could very well be using true legitimately too, in which case it wont tell you:
true := false
...
if condition == true {
// ...
}Good point!
Edit: but then, your expected original variable name, `tue` in my example, would be used while not yet defined:
true := false
if foo() || bar {
tue = true // Compilation error
}
...
if condition() == true {
...
}
Only possible case I can think of: you already defined tue, but tried to redefine it via variable shadowing: tue := true
...
if cond() {
true := false
if foo() || bar() {
tue = true // Will compile, but not the tue you expected
}
...
if condition == true {
...
}
}I think it's crazy that this isn't a compile-time error, but it's not even close to "you can change the global value of builtin values".