加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 18:56:57 所属栏目:大数据 来源:网络整理
导读: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=`rootfuncEcho/funcfuncEcho2/func/
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的反射包,实现根据函数名自动调用函数。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读