duan19850312 2015-10-08 04:26
浏览 433

如何从Go中的字符串方法名称调用另一个包的方法

Suppose I have two package like

-a

-b

a have some methods like this

 func TestOne() { //something }
 func TestTwo() { //something }

I need to call package a's methods from package b but by only string method name. Like i get the string "TestOne" and calls for the method TestOne(). How can i get that done.

Edit : I have Read about reflect. but reflect needs an struct and functions be a member of that struct. What if My functions are not member of a struct? just plain methods in a package. and the calling methods and called methods are in different package. Then?

NB. There could be some methods that have parameters as well.

  • 写回答

2条回答 默认 最新

  • doubaisui2526 2015-10-08 04:41
    关注

    You can try to use reflect in go. This link may be help you https://golang.org/pkg/reflect/ and http://mikespook.com/2012/07/function-call-by-name-in-golang/

    func foo() {
        // bla...bla...bla...
    }
    func bar(a, b, c int) {
        // bla...bla...bla...
    }
    funcs := map[string]interface{}{"foo":foo, "bar":bar}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决