当前位置: 首页 > news >正文

网站咋建立短链接在线生成免费

网站咋建立,短链接在线生成免费,受欢迎的南昌网站建设,wordpress 导购站模板引言 go语言中,如果某个数据类型实现了一系列的方法,如何批量去执行呢,这时候就可以利用反射里的func (v Value) Call(in []Value) []Value 方法。 // Call calls the function v with the input arguments in. // For example, if len(in)…

引言

go语言中,如果某个数据类型实现了一系列的方法,如何批量去执行呢,这时候就可以利用反射里的func (v Value) Call(in []Value) []Value 方法。

// Call calls the function v with the input arguments in.
// For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]).
// Call panics if v's Kind is not Func.
// It returns the output results as Values.
// As in Go, each input argument must be assignable to the
// type of the function's corresponding input parameter.
// If v is a variadic function, Call creates the variadic slice parameter
// itself, copying in the corresponding values.
func (v Value) Call(in []Value) []Value {v.mustBe(Func)v.mustBeExported()return v.call("Call", in)
}

Call方法实际使用时主要有以下两种调用方式:

方法1:使用reflect.Type.Method.Func.Call

package mainimport ("fmt""reflect"
)type dog struct {name string
}func (a *dog) Speak() {fmt.Println("speaking!")
}
func (a *dog) Walk() {fmt.Println("walking!")
}func (a *dog) GetName() (string, error) {fmt.Println("Getting name!")return a.name, nil
}
func main() {var tudou = &dog{name: "tudou"}// 获取reflect.TypeobjectType := reflect.TypeOf(tudou)// 批量执行方法for i := 0; i < objectType.NumMethod(); i++ {fmt.Printf("Now method: %v is being executed...\n", objectType.Method(i).Name)// Call的第一个入参对应receiver,即方法的接受者本身ret := objectType.Method(i).Func.Call([]reflect.Value{reflect.ValueOf(tudou)})fmt.Printf("The return of method: %s is %v\n\n", objectType.Method(i).Name, ret)}
}

注:
1、这里说明下,为什么使用Func调用Call时第一个入参是对应receiver本身method.Func.Call([]reflect.Value{reflect.ValueOf(tudou)})
可以看下结构体MethodFunc的定义,有这么一句注释func with receiver as first argument

/** The compiler knows the exact layout of all the data structures above.* The compiler does not know about the data structures and methods below.*/// Method represents a single method.
type Method struct {...Func  Value // func with receiver as first argument...
}

2、objectType.Method(i)返回的是一个Method结构体

方法2:使用reflect.Value.Method(index).Call

package mainimport ("fmt""reflect"
)type dog struct {name string
}func (a *dog) Speak() {fmt.Println("speaking!")
}func (a *dog) SetName(name string) error {fmt.Println("Setting name!")a.name = namereturn nil
}func (a *dog) GetName() (string, error) {fmt.Println("Getting name!")return a.name, nil
}func main() {var tudou = &dog{name: "tudou"}// 获取reflect.ValueobjectValue := reflect.ValueOf(tudou)// 根据方法名获取method,执行CallobjectValue.MethodByName("Speak").Call(nil)objectValue.MethodByName("SetName").Call([]reflect.Value{reflect.ValueOf("newName")})objectValue.MethodByName("GetName").Call(nil)
}

注:
1、不同于方法1,使用reflect.Value.Method直接调用Call,不需要使用receiver作为第一个入参。可以看下方法MethodByName的注释,有这么一句The arguments to a Call on the returned function should not include a receiver

// MethodByName returns a function value corresponding to the method
// of v with the given name.
// The arguments to a Call on the returned function should not include
// a receiver; the returned function will always use v as the receiver.
// It returns the zero Value if no method was found.
func (v Value) MethodByName(name string) Value {if v.typ == nil {panic(&ValueError{"reflect.Value.MethodByName", Invalid})}if v.flag&flagMethod != 0 {return Value{}}m, ok := v.typ.MethodByName(name)if !ok {return Value{}}return v.Method(m.Index)
}

2、objectValue.MethodByName("Speak")返回的是一个reflect.Value,这个跟方法1调用Method()有明显区别

3、另外值得留意的是,虽然方法2可以参考方法1的for循环批量执行method,但是reflect.Value似乎并没有直接提供方法获取每一个method的Name。但是我们可以根据Index借助reflect.Type.Method(Index).Name来获取Name,这是因为每一个method的NameIndex是一一对应的

type Method struct {// Name is the method name.Name string...Index int   // index for Type.Method
}

其实,注1里的方法func (v Value) MethodByName(name string) Value 里有一段也是根据这个对应关系实现的m, ok := v.typ.MethodByName(name) ... return v.Method(m.Index),有兴趣的同学可以留意观察下

http://www.wangmingla.cn/news/761.html

相关文章:

  • 网站建设的意义百度关键词推广一年多少钱
  • ps做购物小网站独立站seo
  • .com网站备案网络优化器下载
  • 哪家上市公司做视频网站网络推广方案怎么写
  • 做销售平台哪个网站好搜索引擎优化论文3000字
  • 查看网站点击量个人如何做百度推广
  • 国内做的比较好的网站网站统计
  • 专门做娱乐场所的设计网站免费十八种禁用网站
  • 论坛类网站可以做移动端吗有必要买优化大师会员吗
  • 自助网站制作爱站网站排名查询工具
  • 高端大气上档次的网站软文营销推广
  • wordpress评论可见插件seo入门教程网盘
  • 沧州网络运营公司seo网站推广实例
  • 如何判断网站好坏培训机构是干什么的
  • c做项目的网站一键识图找原图
  • 四六级查成绩网站怎么做国际新闻直播
  • 微网站平台微网站建设方案常用的seo查询工具有哪些
  • 做网站平台成本宁德市旅游景点大全
  • 政府网站模板php网址查询工具
  • java php 大型网站开发河南企业网站推广
  • 做标签网站是什么重庆seo网站建设
  • 原有网站已备案 怎么做接入pc网站优化排名软件
  • 上海建设银行青浦分行网站全网关键词云查询
  • 医院网站建设需求分析调研表列表网推广效果怎么样
  • 成都营销型网站建设站内优化怎么做
  • 容桂佛山做app网站引流推广平台软件
  • 建网站石家庄谷歌商店下载安装
  • 怎么给公司做微网站app拉新推广平台有哪些
  • 品牌网站定制开发优化大师最新版本
  • 网站服务器的重要性论坛推广技巧