Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split a single line of code into multiple lines in Go

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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),
    );

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading