package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func (p Person) Greet() string { return fmt.Sprintf("Hello, my name is %s.", p.Name) } func main() { p := Person{"Alice", 25} value := reflect.ValueOf(p) numMethods := value.NumMethod() fmt.Println("Number of methods:", numMethods) // Output: Number of methods: 1 }
package main import ( "fmt" "reflect" ) func add(a, b int) int { return a + b } func main() { value := reflect.ValueOf(add) numMethods := value.NumMethod() fmt.Println("Number of methods:", numMethods) // Output: Number of methods: 0 }In this example, we define a function `add()` that takes two `int` arguments and returns their sum. We then use `reflect.ValueOf()` to obtain a `reflect.Value` object representing the function. In this case, since functions do not have methods, `NumMethod()` returns 0. Both examples use the Go `reflect` package.