package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func (p Person) Greetings() { fmt.Printf("Hello, my name is %s and I am %d years old\n", p.Name, p.Age) } func main() { p := Person{Name: "John", Age: 30} m := reflect.ValueOf(p).MethodByName("Greetings") m.Call(nil) }
package main import ( "fmt" "reflect" ) func main() { s := "hello world" m := reflect.ValueOf(s).MethodByName("ToUpper") result := m.Call(nil)[0].String() fmt.Println(result) }In the above example, we have called the `ToUpper` method on a string by using the `MethodByName` method. We then called the `Call` method on the returned value to get the result of the method call. This example uses the `reflect` package to dynamically call a method on a string.