多行中的多个返回值

tom*_*ole -1 return return-type go

如何在GoLang中多行返回多个值?

  if  x == y {
    req, _ := cgi.Request()
    return req.FormValue("a"),
      req.FormValue("b"),
      req.FormValue("c"),
      req.FormValue("d"),
      req.FormValue("e"),

  } else {
      ...
  }
Run Code Online (Sandbox Code Playgroud)

./example.go:9:3:语法错误:意外},期待表达

icz*_*cza 5

这不是复合文字或函数调用,您不能在最后一行之后放置一个尾随逗号:

return req.FormValue("a"),
  req.FormValue("b"),
  req.FormValue("c"),
  req.FormValue("d"),
  req.FormValue("e")
Run Code Online (Sandbox Code Playgroud)

看一个例子:

func f() (int, int, string) {
    return 1,
        2,
        "3"
}
Run Code Online (Sandbox Code Playgroud)

测试它:

fmt.Println(f())
Run Code Online (Sandbox Code Playgroud)

输出(在Go Playground上试试):

1 2 3
Run Code Online (Sandbox Code Playgroud)

查看相关问题:如何在Golang中打破一长串代码?