当前位置:网站首页>golang-reflect-method-callback
golang-reflect-method-callback
2022-08-02 14:12:00 【FatherOfCodingMan】
参考:
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.
边栏推荐
猜你喜欢
随机推荐
MATLAB drawing command fimplicit detailed introduction to drawing implicit function graphics
Problems related to prime numbers - small notes
The SSE instructions into ARM NEON
Unity-存档与读档
深入理解Mysql索引底层数据结构与算法
General code for pytorch model to libtorch and onnx format
永久更改pip源
Happy, 9/28 scene collection
Unity-Ads广告插件
奇技淫巧-位运算
Redis common interview questions
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
MATLAB绘图命令fimplicit绘制隐函数图形入门详解
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
How to simulate 1/3 probability with coins, and arbitrary probability?
Open the door of electricity "Circuit" (1): voltage, current, reference direction
1. Development community homepage, register
倍增和稀疏表
第三十一章:二叉树的概念与性质
【离散化+前缀和】Acwing802. 区间和









