Home > Back-end >  How to split a single line of code into multiple lines in Go
How to split a single line of code into multiple lines in Go

Time:01-11


Is there any way to split a single, really long, line of code into multiple lines. But it would still be treated as a single line of code by compiler.

For example in C or in Python, there's \ which let's us split the same line into multiple lines.
C example code:

int min(int a, int b) {
    return a<b ? a : b;
}

int main() {
    int ans = min(4, \
                    5 \
                );
    cout << ans << endl;
}

Here even when I broke the same code min(4,5) into multiple lines it worked.

I tried the same in go but it gave me an error, wondering if there's anyway to achieve the same.
Current go code:

return min( \
    query(2*curNode, l, mid, qL, qR, n, st), \
    query(2*curNode 1, mid 1, r, qL, qR, n , st) \
);

Getting following error

solution.go:37:17: invalid character U 005C '\'
solution.go:38:50: invalid character U 005C '\'
solution.go:39:54: invalid character U 005C '\'
solution.go:39:55: syntax error: unexpected newline, expecting comma or )

NOTE: query is just a helper, recursive method, (for querying segment tree) which returns int. And min if a function similar to the one in c example.

EDIT: Paul and Mondo's suggestions worked, this will also work:

return min(
        query(2*curNode, l, mid, qL, qR, n, st),
        query(2*curNode 1, mid 1, r, qL, qR, n , st),
    );

CodePudding user response:

Option 1:

return min(
    query(2*curNode, l, mid, qL, qR, n, st),
    query(2*curNode 1, mid 1, r, qL, qR, n, st),
)

Option 2 (preferred):

return min(
    query(2*curNode, l, mid, qL, qR, n, st),
    query(2*curNode 1, mid 1, r, qL, qR, n, st))
  •  Tags:  
  • Related