Why does Golang enforce curly bracket to not be on the next line?

前端 未结 3 1777
野趣味
野趣味 2021-02-01 15:14

correct:

if(true) {

}

incorrect:

if(true)
{

}

Why is this style enforced, does it have something to do with

3条回答
  •  孤独总比滥情好
    2021-02-01 15:29

    It has to do with the Spec, i.e. it's not just something they built into their compilers

    Semicolons

    The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:

    When the input is broken into tokens, a semicolon is automatically inserted into the token stream at the end of a non-blank line if the line's final token is

    • an identifier
    • an integer, floating-point, imaginary, rune, or string literal
    • one of the keywords break, continue, fallthrough, or return
    • one of the operators and delimiters ++, --, ), ], or }

    To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

    To reflect idiomatic use, code examples in this document elide semicolons using these rules.

    As far as I grasped it from their talks, they wanted to get rid of formatting-discussions and extended the idea with the greation of gofmt

提交回复
热议问题