如何确定函数返回的值的数量

Fel*_*tem 0 reflection go

如何获取函数的返回值长度,效果类似如下:

func Foo() (int, int){
    ...
}
func Bar() (int, float, bool, string, error){
    ...
}
n1 := getReturnLength(Foo) // shall be 2
n2 := getReturnLength(Bar) // shall be 5
Run Code Online (Sandbox Code Playgroud)

我该如何实现getReturnLength功能或平等的功能?

Pra*_*een 9

您可以使用反射包来实现它.

reflect.TypeOf(Foo).NumOut()
Run Code Online (Sandbox Code Playgroud)

您可以在https://golang.org/pkg/reflect/#Type上了解反映包