package main
import "fmt"
import "reflect"
import "encoding/xml"
type st struct{
}
func (this *st)Echo(){
fmt.Println("echo()")
}
func (this *st)Echo2(){
fmt.Println("echo--------------------()")
}
var xmlstr string=`<root>
<func>Echo</func>
<func>Echo2</func>
</root>`
type st2 struct{
E []string `xml:"func"`
}
func main() {
s2 := st2{}
xml.Unmarshal([]byte(xmlstr), &s2)
s := &st{}
v := reflect.ValueOf(s)
v.MethodByName(s2.E[1]).Call(nil)
}
利用golang的反射包,实现根据函数名自动调用函数。
本文介绍如何利用Go语言的反射包来实现根据函数名自动调用函数的功能,通过实例演示了如何解析XML字符串并获取函数名,然后通过反射调用相应的函数。
932

被折叠的 条评论
为什么被折叠?



