You are painting with a pretty broad brush, can you provide a concrete example of these?
You are painting with a pretty broad brush, can you provide a concrete example of these?
if (cond) {
return x
} else {
return y
}
works, but if (cond) { return x }
else { return y }
doesn't.hmm, what's this then?
You'd think one would want to look for it in that instead of an FAQ page.
The section you linked does explain it, btw.
IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
Note there is no newline or semicolon between Block and the optional “else” clause, and a Block is thusly defined: Block = "{" StatementList "}" .
So that being the case I’m not sure why you’d assume it’s valid to put a newline after the trailing “}” of the Block, before the “else”.The other factor at play here is the treatment of semicolons: https://go.dev/ref/spec#Semicolons
All of this is quite natural once you use the language. in fact, it was designed this way in response to the way it was being used.
It is not appropriate for the spec to constantly reiterate concepts that have already been introduced, but rather it should precisely define each concept once. If you want to learn a language from its spec then it behooves you to consider the document as a whole. That requires a lot of mental overhead and I think most people are better off learning from examples and tutorials rather than reading a language spec.
So your example gets transformed to:
if (cond) { return x };
else { return y };
So it is mentioned in the language specification, just as a non-obvious consequence of this.It's not my most favourite part of the language either, but at least the compiler message on it is better now (it used to complain about semicolons, which was confusing because they're not there in your source code; now it just says "unexpected else, expecting }", which is still a bit confusing but better).
I'm fairly sure The Go Programming Language book mentions this somewhere near the beginning, but some of the online tutorial/tour perhaps could be improved. There's always a bit of a balance in how much to mention.