package main import ( "fmt" "reflect" ) func foo(x int, y string) { fmt.Println(x, y) } func main() { typ := reflect.TypeOf(foo) numIn := typ.NumIn() fmt.Println(numIn) // Output: 2 }
package main import ( "fmt" "reflect" ) type Foo struct{} func (f *Foo) bar(x int, y string) { fmt.Println(x, y) } func main() { f := &Foo{} typ := reflect.TypeOf(f) method, _ := typ.MethodByName("bar") numIn := method.Type.NumIn() fmt.Println(numIn) // Output: 3 }In this example, we define a struct `Foo` with a method `bar` that takes two parameters `x` and `y`. We use `reflect.TypeOf` to get the type information for an instance of `Foo` and `MethodByName` to retrieve the type information for the `bar` method. We then use the `NumIn` field of the method's type information to determine that the method takes three input parameters, including the implicit `receiver` parameter. Package library: `reflect` (part of the standard library).