将代码分行分享。

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

Share code in severel lines

问题

为了提高可读性,我将方法链分成多行:

for _, e := range methods {
    switch e {
    case "GET":
        if len(name) > 0 {
            router.Handle(path, httpAjaxFunc(handler)).
                Methods(e).
                Headers("X-Requested-With", "XMLHttpRequest", "Payload", "").
                Name(name[0])
        } else {
            router.Handle(path, httpAjaxFunc(handler)).
                Methods(e).
                Headers("X-Requested-With", "XMLHttpRequest", "Payload", "")
        }
    }
}

但是我在这里遇到了一个编译错误:

在第29行,文件controllers\routes\funcs.go语法错误:意外的.(点号)

我该如何将其分成多行?

英文:

For readability, I split method chaining into multi lines:

for _, e := range methods {
	switch e {
	case "GET":
		if len(name) > 0 {
			router.Handle(path, httpAjaxFunc(handler))
				  .Methods(e)
				  .Headers("X-Requested-With", "XMLHttpRequest", "Payload", "")
			      .Name(name[0]);
		} else {
			router.Handle(path, httpAjaxFunc(handler))
			      .Methods(e)
			      .Headers("X-Requested-With", "XMLHttpRequest", "Payload", "");
		}

<but I've got here a compiler error:

> at line 29, file controllers\routes\funcs.gosyntax error: unexpected .

How can I split it in multi lines?

答案1

得分: 2

这有点丑陋,但是我知道的唯一方法是:

for _, e := range methods {
    switch e {
    case "GET":
        if len(name) > 0 {
            router.Handle(path, httpAjaxFunc(handler)).
                  Methods(e).
                  Headers("X-Requested-With", "XMLHttpRequest", "Payload", "").
                  Name(name[0])
        } else {
            router.Handle(path, httpAjaxFunc(handler)).
                  Methods(e).
                  Headers("X-Requested-With", "XMLHttpRequest", "Payload", "")
        }
英文:

This is kind of ugly, but the only way I know of:

for _, e := range methods {
    switch e {
    case &quot;GET&quot;:
        if len(name) &gt; 0 {
            router.Handle(path, httpAjaxFunc(handler)).
                  Methods(e).
                  Headers(&quot;X-Requested-With&quot;, &quot;XMLHttpRequest&quot;, &quot;Payload&quot;, &quot;&quot;).
                  Name(name[0])
        } else {
            router.Handle(path, httpAjaxFunc(handler)).
                  Methods(e).
                  Headers(&quot;X-Requested-With&quot;, &quot;XMLHttpRequest&quot;, &quot;Payload&quot;, &quot;&quot;)
        }

huangapple
  • 本文由 发表于 2015年2月18日 21:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/28585230.html
匿名

发表评论

匿名网友

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

确定