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),
}
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),
}
what can I do ?
As FUZxxl pointed out, your problem is the automatic insertion of semicolons. The spec says:
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:Break after beginning of parameter list instead of before the function:
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:I haven't thought enough with this example.
f(...).f(...)
is of course not possible, as the return value off
has no memberf
. One would have to reassignf
. So you gain nothing from that.I would probably write some variant of:
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.