package main
import (
"fmt"
"reflect"
)
type Type1 struct{}
func (t Type1) Method1() {
fmt.Println("Method 1")
}
type Type2 struct{}
func (t Type2) Method2(args0 string, args1 float64) {
fmt.Println("Method 2 ", args0, args1)
}
func Invoke(any interface{}, name string, args ...interface{}) {
inputs := make([]reflect.Value, len(args))
for i, _ := range args {
inputs[i] = reflect.ValueOf(args[i])
}
reflect.ValueOf(any).MethodByName(name).Call(inputs)
}
func main() {
Invoke(Type1{}, "Method1")
Invoke(Type2{}, "Method2", "abc", 3.145926)
}
08-19
1637
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-19
894
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)