package main
import "fmt"
import "reflect"
type C struct {
A string
}
func (c *C) Read() error {
fmt.Println("C.Read" )
return nil
}
type D struct {
B string
}
func (d *D) Read() string {
fmt.Println("D.Read" )
return d.B
}
func (c *C) ReadOther(inf interface{}) error {
ref := reflect.ValueOf(inf)
method := ref.MethodByName("Read")
if method.IsValid() {
r := method.Call(make([]reflect.Value,0))
fmt.Println(r[0].String())
}
return nil
}
func main() {
c := &C{A:"iii"}
d := &D{B:"ddd"}
c.ReadOther(d)
}
golang类之间方法调用
最新推荐文章于 2023-04-25 15:33:34 发布