Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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

for example,

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

or simply,

a = b + c + s +
    x + y

Is it possible to do that in Go language? Thanks

like image 653
Viele Avatar asked Jun 23 '11 20:06

Viele


People also ask

How do you write multi line string in Golang?

Multiline strings Creating a multiline string in Go is actually incredibly easy. Simply use the backtick ( ` ) character when declaring or assigning your string value. str := `This is a multiline string.

How do you use a multi line statement?

For example, n = 50 is an assignment statement. Multi-Line Statements: Statements in Python can be extended to one or more lines using parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash (\).

How do you write multiple lines?

What you do put your cursor where you want to add a character on a line, then use SHIFT+ALT and either the arrow keys or your mouse (you have to click to the same column position the line that you are selecting to) to select all the lines that you want to edit the same way.

What is multiline statement?

PythonServer Side ProgrammingProgramming. Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example − total = item_one + \ item_two + \ item_three.


2 Answers

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.

like image 59
tux21b Avatar answered Sep 22 '22 21:09

tux21b


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.

like image 35
tylerl Avatar answered Sep 23 '22 21:09

tylerl