Does a go block following a for, func or if statement have to have the opening brace on the same line? I get a compile error if I move it down but I can’t see in the language spec where they show that a block has to be structured like that.
A block is a sequence of declarations and statements within matching
brace brackets.Block = “{” { Statement “;” } “}” .
IfStmt = “if” [ SimpleStmt “;” ] Expression Block [ “else” ( IfStmt |
Block ) ] .
From Effective Go, because of semicolon inference:
As jnml comments, the language syntax is correct for blocks.
But combined with Semicolon injection, it means you should really:
if‘ won’t do what you think it should)gofmtand don’t think about it (Preferably, gofmt your code each time you save it in your editor. It is fast and will make your code consistent with the rest of any Go code out there)Even the Go compiler will enforce that “same line for brace” rule, to avoid any unforeseen side-effect.
So the language reference doesn’t say where to put the brace, but both
gofmtand the compiler will make sure it is correctly placed for aifstatement.