当前位置:网站首页>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.
边栏推荐
- pygame image rotate continuously
- Flink + sklearn - use JPMML implement flink deployment on machine learning model
- Article pygame drag the implementation of the method
- Redis common interview questions
- Network Security Packet Capture
- LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
- Detailed introduction to drawing complex surfaces using the plot_surface command
- Test case exercises
- 快速排序
猜你喜欢

mysql学习总结 & 索引

基于矩阵计算的线性回归分析方程中系数的估计

Yolov5 official code reading - prior to transmission

mysql的索引结构为什么选用B+树?

2.登录退出,登录状态检查,验证码

Detailed introduction to the hierarchical method of binary tree creation

How to simulate 1/3 probability with coins, and arbitrary probability?

剑指offer:合并两个排序的链表

剑指offer:在O(1)时间删除链表结点

二叉排序树与 set、map
随机推荐
unity-shader(中级)
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
Installation and configuration of Spark and related ecological components - quick recall
Unity-Ads广告插件
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
MATLAB图形加标注的基本方法入门简介
队列与栈
冷读123
6. Unified logging
unity-shader(入门)
cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
Open the door of electricity "Circuit" (1): voltage, current, reference direction
快速排序
Lightweight AlphaPose
pygame image rotate continuously
GMP scheduling model of golang
Based on the least squares linear regression equation coefficient estimation
Manifest merger failed : Attribute [email protected] value=
Redis的线程模型
Software Testing Basics (Back)