Search code examples
goreturnreturn-type

Multiple return values in multiple lines


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


Solution

  • 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?