3 ms·
The Go compiler inserts semi-colons. https://go.dev/ref/spec#Semicolons https://go.dev/ref/spec#Semicolons If you follow those rules, you will see that this:
by arp242 2y ago
The Go compiler inserts semi-colons. https://go.dev/ref/spec#Semicolons https://go.dev/ref/spec#Semicolons
If you follow those rules, you will see that this:
if true;
{
fmt.Println()
}
else
{
fmt.Println()
}
Will get rewritten to:
if true;
{
fmt.Println();
};
else;
{
fmt.Println();
};
And that won't work. That's why the braces need to be as "} else {". and "if .. {".
It used to be that the compiler gave some pretty confusing errors about semi-colons on this, but it seems that's been improved now.
JavaScript has similar semi-colon insertion by the way, but with some different (more confusing) rules.
- xandrius 2y agoThat's really neat, it had escaped me till now. Thanks for the explanation (today I'm one of the lucky 10,000!)