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

huangapple go评论82阅读模式
英文:

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

问题

有没有办法将一行非常长的代码拆分成多行,但编译器仍将其视为单行代码。

例如,在C++或Python中,有一个\符号,它允许我们将同一行代码拆分成多行。

C++示例代码:

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

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

即使我将代码min(4,5)拆分成多行,它仍然可以正常工作。

我尝试在Go中做同样的事情,但是它给了我一个错误,想知道是否有办法实现相同的效果。

当前的Go代码:

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

得到以下错误:

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 )

注意query只是一个辅助的递归方法(用于查询线段树),返回一个整数。而min是类似于C++示例中的函数。

编辑:Paul和Mondo的建议有效,下面的代码也可以工作:

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

<br>
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. <br>
C++ example code:

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

int main() {
    int ans = min(4, \
                    5 \
                );
    cout &lt;&lt; ans &lt;&lt; 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.<br>
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 &#39;\&#39;
solution.go:38:50: invalid character U+005C &#39;\&#39;
solution.go:39:54: invalid character U+005C &#39;\&#39;
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),
    );

答案1

得分: 5

选项1:

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

选项2(首选):

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

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

答案2

得分: 0

你可以在逗号或句点处分割长行。
记住,如果是逗号,有时你需要在最后一行添加逗号,这取决于你正在做什么。
(例如:向切片添加元素)

英文:

You can split long lines at a comma or at a dot.
remember if it is a comma, sometimes you have to add a comma at the last line, depending on what you are doing.
(For example: adding elements to a slice)

huangapple
  • 本文由 发表于 2022年1月11日 01:32:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/70656604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定