golang-reflect-method-callback

参考:

pass-method-argument-to-function

通过反射调用method方法

package main
​
import (
    "fmt"
    "reflect"
)
​
type Foo struct {
    i int
}
​
func (f Foo) A(b int) {
    fmt.Println("testA", b, f.i)
}
func (f Foo) B(b int) {
    fmt.Println("testB", b, f.i)
}
func (f Foo) C(b int) {
    fmt.Println("testC", b, f.i)
}
​
func main() {
    var f Foo
    f.i = 10
    bar := func(name string, b int) {
        params := make([]reflect.Value, 0)
        params = append(params, reflect.ValueOf(b))
        //TODO: maybe you need to check function parameters.
        reflect.ValueOf(f).MethodByName(name).Call(params) //如果没有参数: params = nil
    }
    bar("A", 1)
    bar("B", 2)
    bar("C", 3)
​
    f.i = 999
    t := func(m func(int), b int) {
        m(b)
    }
​
    t(f.A, 888)
}

可以在 playground 上运行。

testA 1 10
testB 2 10
testC 3 10
testA 888 999

上面方法中如果对象接收器是指针,那么MethodByName前的对象也要是指针

func (f *Foo) A(b int) {
    fmt.Println("testA", b, f.i)
}
//相应
reflect.ValueOf(&f).MethodByName(name).Call(params) 

interface的基础1

package main
​
import (
    "fmt"
    "reflect"
)
​
type base interface {}
​
type Foo interface {
    Cose() string
    SetV(string)
}
​
type Bar struct {
    cose string
}
​
//类型接收器是指针
func (b *Bar) Cose() string {
    return b.cose
}
​
func (b *Bar) SetV(v string) {
     b.cose = v
}
​
type Bar2 struct {
    cose string
}
​
//类型接收器是对象
func (b Bar2) Cose() string {
    return b.cose
}
​
func (b Bar2) SetV(v string) {
     b.cose = v
}
​
func passbyv(b Foo) {
     b.SetV("hello")
}
​
func passbyp(p *Foo) {
     (*p).SetV("Scott")
}
​
func main() {
    bar := Foo(&Bar{
        cose: "ciaone",
    })
    fmt.Println(reflect.TypeOf(bar))
    ii, ok := bar.(Foo)
    if !ok {
        panic("type assertion")
    }
    fmt.Println(" cose : " + ii.Cose())
​
    passbyv(bar)
    fmt.Println(" b v: " + ii.Cose())
    passbyp(&bar)
    fmt.Println(" b p: " + ii.Cose())
​
    bb := Foo(Bar2{
        cose: "bar22",
    })
    fmt.Println(reflect.TypeOf(bb))
​
    b2, ok := bb.(Foo)
    if !ok {
        panic("type assertion")
    }
​
    passbyv(b2)
    fmt.Println(" b v: " + b2.Cose())
    passbyp(&b2)
    fmt.Println(" b p: " + b2.Cose())
}
*main.Bar
 cose : ciaone
 b v: hello
 b p: Scott
main.Bar2
 b v: bar22
 b p: bar22

可以在这里运行。

interface装struct对象或struct对象指针,是用指针还是对象,要看这个struct定义方法时类型接收器用的是对象还是指针。

给函数传interface变量时,实际传值还是指针只与interface装的值是什么有关。

Interface values

实际上,接口值可以看作是一个值和一个具体类型的元组:

(value, type)

An interface value holds a value of a specific underlying concrete type.

Calling a method on an interface value executes the method of the same name on its underlying type.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FatherOfCodingMan

如果觉得有用的话,可以赏点饭钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值