如何获取函数的返回值长度,效果类似如下:
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功能或平等的功能?
您可以使用反射包来实现它.
reflect.TypeOf(Foo).NumOut()
Run Code Online (Sandbox Code Playgroud)
您可以在https://golang.org/pkg/reflect/#Type上了解反映包