0

How to return multiple Values in multiple lines in GoLang?

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

  } else {
      ...
  }

./example.go:9:3: syntax error: unexpected }, expecting expression

tomole
  • 927
  • 3
  • 12
  • 35

1 Answers1

5

This is not a composite literal or function call, you must not put a trailing comma after the last line:

return req.FormValue("a"),
  req.FormValue("b"),
  req.FormValue("c"),
  req.FormValue("d"),
  req.FormValue("e")

See an example:

func f() (int, int, string) {
    return 1,
        2,
        "3"
}

Testing it:

fmt.Println(f())

Output (try it on the Go Playground):

1 2 3

See related question: How to break a long line of code in Golang?

icza
  • 389,944
  • 63
  • 907
  • 827