我在结构定义下有一个带有 2 个方法的结构,我想在其他使用结构名称和方法名称作为参数的地方调用。
结构代码如下:
type ArticleForm struct {
Name string `required:"true" pattern:"^[A-Za-z0-9\u4e00-\u9fa5]{1,1024}$" valid:"Required;MaxSize(1024)"`
Category []Category `class:"multis" required:"true" valid:"Required" optionqs:"GetCategoryOption"`
Content string `class:"wysiwg_area" required:"true" valid:"Required"`
Tags []Tags `class:"multis_create" optionqs:"GetTagOptions"`
}
方法定义如下:
func (this *ArticleForm) GetTagOptions() []Tags {
return GetTagsOptions(nil)
}
以下是我想调用的内容:
func main() {
s := "models.ArticleForm"
t := "GetTagOptions"
//following is the question, how can i exec following?
funcall(s,t)
}
如何实现
funcall(s,t)
? 最佳答案
调用由其名称给出的某种类型的方法很容易(使用 reflection )。看这个例子:
type My struct{}
func (m My) MyFunc() {
fmt.Println("MyFunc called")
}
func main() {
m := My{}
meth := reflect.ValueOf(m).MethodByName("MyFunc")
meth.Call(nil)
}
输出(在 Go Playground 上试试):
MyFunc called
“实例化”给定字符串名称的类型是不可能的,因为如果您的代码没有明确引用该类型,它甚至可能不会包含在可执行二进制文件中。详情参见 Call all functions with special prefix or suffix in Golang ;和 Splitting client/server code 。
一种可能的解决方法是使用某种“类型注册表”,在使用它之前填充它(在您想通过其名称创建值之前)。类型注册表(可能是映射)可以保存
reflect.Type
值或从类型名称映射的工厂函数。按照上面的
My
类型声明,存储 reflect.Type
值的类型注册表可能如下所示(在 Go Playground 上试试):registry := map[string]reflect.Type{
"My": reflect.TypeOf((*My)(nil)).Elem(),
}
v := reflect.New(registry["My"]).Elem()
v.MethodByName("MyFunc").Call(nil)
存储工厂函数的注册表可能如下所示(在 Go Playground 上试试):
registry := map[string]func() interface{}{
"My": func() interface{} { return My{} },
}
v := registry["My"]()
meth := reflect.ValueOf(v).MethodByName("MyFunc")
meth.Call(nil)
关于go - 如何通过字符串名称和字符串结构名称调用方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42906759/