dqhsv0147421 2016-03-07 05:26
浏览 769

如何使用Golang将结构体的方法作为参数传递给另一个函数

Sorry, I post my question again.

I have been read the solution before I ask my question. I think it can not help me because my question is how to pass a function as a parameter? I don't want to call it.

I just want to pass it to another function that I can't edit (or I don't want to edit), and I want to use a string variable to point the function

funcName := "Go"
 m.set(t.funcName)

I think this is different from this question Call a Struct and its Method by name in Go?

for example

I have a function like:

type Context struct{}
type myclass struct{}
type Handler func (c *Context)

func (r *myclass) set(ch Handler)  {

}

I can use this way:

type testclass struct {}
func (t *testclass) Go(c *Context){
    println("Hello");
}

t := &testclass{}
m := &myclass{}
m.set(t.Go)

and my question is

type testclass struct{}
func (t *testclass) Go(c *Context){
    println("Hello");
}

t := &testclass{}
m := &myclass{}

funcName := "Go"
m.set(t.funcName)

any way can do this?

reflect?or what?

if it is impossible, there are other ways to do this?

thanks

  • 写回答

1条回答 默认 最新

  • dpfwhb7470 2016-03-07 06:20
    关注

    You can get the method by name using the reflect package. Here's a function to get a Handler given the name:

    func handlerByName(v interface{}, name string) (Handler, error) {
      m := reflect.ValueOf(v).MethodByName(name)
      if !m.IsValid() {
        return nil, errors.New("method not found")
      }
      h, ok := m.Interface().(func(*Context))
      if !ok {
        return nil, errors.New("method is not a handler")
      }
      return h, nil
    }
    

    Here's how to use the function:

    h, err := handlerByName(t, "Go")
    if err != nil {
       // handle error
    }
    m.set(h)
    

    playground example

    Note that the function returned by handlerByName is a reflection wrapper around the original function (thanks @OneOfOne for pointing this out). Calling the wrapper is slow compared to calling the function directly.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?