Alex Netkachov
Alex Netkachov

Reputation: 13522

Write long statement in multiple lines

I have a quite long func declaration that I would like to split into several lines:

func LongMethodName(param1 *Type1, param2 *Type2, param3 *Type3) (param4 *Type1, param5 *Type2, param6 *Type3) {
    ...
    return
}

it is quite unmanageable.

Is there a way of writing a function declaration as follows?

func LongMethodName(param1 *Type1, param2 *Type2, param3 *Type3)
    (param4 *Type1, param5 *Type2, param6 *Type3)
{
    ...
    return
}

Upvotes: 3

Views: 399

Answers (1)

Soheil Hassas Yeganeh
Soheil Hassas Yeganeh

Reputation: 1379

This is a result of the semicolon insertion rule: http://golang.org/ref/spec#Semicolons. Go automatically inserts a semicolon right at the end of the first line:

func LongMethodName(param1 *Type1, param2 *Type2, param3 *Type3);
    (param4 *Type1, param5 *Type2, param6 *Type3) {

The first line with the semicolon is actually a valid go expression: It's an external function. Then, it tries to parse the second line and fails!

You can wrap it either by keeping the opening parenthesis on the first line:

func LongMethodName(param1 *Type1, param2 *Type2, param3 *Type3) (
    param4 *Type1, param5 *Type2, param6 *Type3) {
}

or by keeping a comma on the first line:

func LongMethodName(param1 *Type1, param2 *Type2,
    param3 *Type3) (param4 *Type1, param5 *Type2, param6 *Type3) {
}

Both are valid in gofmt.

Upvotes: 4

Related Questions