func add(a, b int) int { return a + b } func main() { fn := reflect.ValueOf(add) numOut := fn.Type().NumOut() fmt.Println(numOut) } // Output: 1
type Person struct { Name string Age int } func (p Person) String() string { return fmt.Sprintf("%s (%d)", p.Name, p.Age) } func main() { p := Person{Name: "Alice", Age: 30} fn := reflect.ValueOf(p.String) numOut := fn.Type().NumOut() fmt.Println(numOut) } // Output: 1In this example, we define a `Person` struct and a `String()` method that returns a formatted string representation of the person's name and age. We create a new `Person` object and obtain a `reflect.Value` object that represents the `String()` method. We call `NumOut()` to get the number of output parameters, which is again 1. Package library: `reflect`