In Go, how to write a multi-line statement?

后端 未结 2 998
你的背包
你的背包 2021-01-01 09:46

In python, we use backslash to indicate that the current statement continues to next line

for example,

a = b + c + s \\
    + x + y

相关标签:
2条回答
  • 2021-01-01 10:27

    Interestingly, the the Go language specification itself requires semicolons at the end of each statement, but the lexer will insert implicit semicolons at the end of lines that look like statements immediately before compilation.

    Therefore, to prevent the unwanted semicolon at the end of an unfinished line, all you need to do is ensure that the line doesn't end with something that could make it look like a complete statement.

    In other words, avoid ending an incomplete line in a variable, constant, function, keyword, or postfix operator (e.g. ++).

    What does that leave? Well, a few things come to mind -- an infix operator (e.g. = or +), a comma, or an opening paren or brace or bracket, for example.

    0 讨论(0)
  • 2021-01-01 10:34

    Sure it is, just put an operator at the end, for example:

    a = b + c + s +
        x + y
    

    Also note that it's not possible to break the line before the operator. The following code is invalid:

    a = b + c + s
        + x + y
    

    The rule is described here and in the specification.

    0 讨论(0)
提交回复
热议问题