Like C, Go’s formal grammar uses semicolons to terminate statements.
But unlike in C, those semicolons do NOT appear in the source. Instead the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them.
The rule could be summarized as, “if the newline comes after a token that could end a statement, insert a semicolon”.
A semicolon can also be omitted immediately before a closing brace.
need semicolons
- Idiomatic Go programs have semicolons ONLY in places such as
for
loop clauses, to separate the initializer, condition, and continuation elements. - They are also necessary to separate multiple statements on a line, should you write code that way.