英文:
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 "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", "")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论