您如何在多行代码中链接命令? [英] how do you chain commands on several lines in go?
问题描述
我想通过这种方式链接命令:
I want to chain commands this way :
var cmdGroups = []*commands.CmdGroup {
commands.MakeCmdGroup("foo", cmd1, cmd2, cmd3).AddConstraint(cmd1, cmd2).AddConstraint(cmd2, cmd1, cmd3),
commands.MakeCmdGroup("bar", cmd1, cmd4).AddConstraint(cmd1, cmd4),
}
出于80列长度的原因,我想将链分成几行,但是Go不允许我编译它:
I'd like to split my chains on several lines for 80-column-lengths reasons, but Go won't let me compile this :
var cmdGroups = []*commands.CmdGroup {
commands.MakeCmdGroup("foo", cmd1, cmd2, cmd3)
.AddConstraint(cmd1, cmd2)
.AddConstraint(cmd2, cmd1, cmd3),
commands.MakeCmdGroup("bar", cmd1, cmd4)
.AddConstraint(cmd1, cmd4),
}
我该怎么办?
推荐答案
正如FUZxxl指出的那样,您的问题是分号的自动插入.规范说:
As FUZxxl pointed out, your problem is the automatic insertion of semicolons. The spec says:
当输入被分解为标记时,将自动使用分号如果非空白行的末尾插入到令牌流中,该行的最终令牌是
When the input is broken into tokens, a semicolon is automatically inserted into the token stream at the end of a non-blank line if the line's final token is
- 标识符
- 整数,浮点数,虚数,符文或字符串文字
- 其中一个关键字会中断,继续,失败或返回
- 运算符和定界符++,-,),]或}
您有一个函数调用,该函数计数为)
,因此在行的末尾添加了分号.
You have a function call, which counts for a )
so a semicolon is added at the end of the line.
要避免自动分号转换,您可以使用以下任一方式编写呼叫方式:
To circumvent automatic semicolon conversion you can write your calls in one of the following ways:
使用.
代替分号:
x.
Method(p1,p2,p3)
在参数列表开头而不是函数之前中断:
Break after beginning of parameter list instead of before the function:
x.Method(
p1,p2,p3, // , at the end is important to prevent semicolon insertion
)
如果您不喜欢上述方法,则可以(从go1.1开始)将这些方法视为第一类市民并临时创建可能更短的快捷方式:
If you dislike the methods above, you can (as of go1.1) treat the methods as first class
citizens and temporarily create shortcuts which might be shorter:
f = x.Method
f(p1,p2,p3).f(p3,p4,p5)
我还没想好这个例子.当然不可能使用 f(...).f(...)
,因为 f
的返回值没有成员 f
.必须重新分配 f
.这样您一无所获.
I haven't thought enough
with this example. f(...).f(...)
is of course not possible, as the return value of f
has no member f
.
One would have to reassign f
. So you gain nothing from that.
这篇关于您如何在多行代码中链接命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!