how do you chain commands on several lines in go?

一个人想着一个人 提交于 2019-12-04 10:41:54
nemo

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

  • an identifier
  • an integer, floating-point, imaginary, rune, or string literal
  • one of the keywords break, continue, fallthrough, or return
  • one of the operators and delimiters ++, --, ), ], or }

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:

Use the . instead of semicolon:

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
)

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)

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.

I would probably write some variant of:

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

However, such long selector operator chains are not to be seen in idiomatic code too often. (I consider the standard library an informal guide to idiomatic code). Perhaps there might be some weakness in this code design/structure.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!