利用golang的反射包,实现根据函数名自动调用函数。

来源:互联网 发布:ssh 连接指定端口 编辑:程序博客网 时间:2024/06/07 15:25
package mainimport "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的反射包,实现根据函数名自动调用函数。
0 0
原创粉丝点击