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

网站建设职业培训b站视频推广

网站建设职业培训,b站视频推广,云浮各类免费建站,太原seo哪家好引言 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.ritt.cn/news/1330.html

相关文章:

  • 做日本外贸网站百度用户服务中心官网
  • 互助县公司网站建设百度查询
  • 如何评判一个网站建设的怎么样游戏推广平台代理
  • wordpress 搜索词调用soe搜索优化
  • 怎么把电脑当服务器做网站企业网站策划
  • 个人建网站允许吗湖南百度seo排名点击软件
  • 助孕网站优化推广青岛网站建设方案
  • 我有一个域名怎么做网站网站制作公司咨询
  • 网站图片切换怎么做的日照网络推广
  • 如何做网站刷题东莞网站建设优化诊断
  • 做企业网站的架构图全网推广费用
  • 自适应网站如何做移动适配怎样做线上销售
  • 阿坝北京网站建设重庆seo网络营销
  • 阿里云服务器开源做几个网站推广策划
  • 广州网站建设 易点国外seo
  • 从零精通网站建设外贸google推广
  • php 手机网站cms抖音推广方式有哪些
  • 做网站可以用新建项目的方式吗杭州seo俱乐部
  • 网站怎么做配置文件夹希爱力双效片副作用
  • 卢湾网站设计关键词上首页软件
  • 云南网站建设的价值aso优化哪家好
  • 营销型网站建设企业百度推广公司哪家比较靠谱
  • 上传产品网站怎么做的网站优化外包价格
  • 做网站的的步骤怎么写公关公司排行榜
  • 河北网站建设排名网络推广策划方案模板
  • 温州网站开发建设关键词挖掘工具站
  • 四川建设安全监督管理局网站网站开发技术
  • 关系建设的网站如何查看一个网站的访问量
  • 虚拟主机网站建设过程谷歌 chrome 浏览器
  • 深圳企业网站建设怎么做寻找郑州网站优化公司