Search code examples
gotypesreturn-valueauto

missing go type for returned values


How come no warnings are issued for the following code?

$ cat ret.go
package main
import "fmt"
func foobar(x int, y int) (z, w int) {
    if x+y > 100 {
         _,w = 3,5
    } else {
        _,w = "MMM",9
    }
    return z,w
}

func main() {
    var x int
    _,x = foobar(42,13)
    fmt.Println(x)
}
$ go build -gcflags=-l ret.go

For the least, the go compiler should know the size of z right?


Solution

  • In golang, you can define multiple variable in one line like next:

    var identifier1, identifier2 type
    

    So, z, w here both declared as int.

    Additional, see this:

    The return values of a function can be named in Golang

    Then, if you not assign a value to z, it will has a default value of int, that is 0. So, no warning, the code is ok.