dougu0824 2019-07-13 08:25
浏览 75
已采纳

根据map [string] somestruct调用golang调度方法

Assuming I have lots of functions or methods with receiver, each of which has different type of parameters. I want to use table-driven method to dispatch function or method call. So I'll construct a table like this:

type command struct {
   name string
   handler func(parameter ...interface{}) // I don't know whether to use `...interface{}` is correct
}

table := map[string]command { ... }

func (c command)foo(f1 int, f2 string) {}
func (c command)bar(b1 bool, b2 int, b3 string) {}
// methods and so on

No matter what command I receive, I'll always use one statement like table[some-command-name](parameter1, parameter2, ...) instead of using the ugly switch-case structure.

Is this possible ? That different methods have different number of parameters is a problem ?

  • 写回答

1条回答 默认 最新

  • dragon8837 2019-07-13 09:00
    关注

    There are two possible ways. One is through reflection, that is reflect package and in particular the Call method. The second one is through function values and closures. I would not recommend the first solution, reflection is generally discouraged because it is complicated, error prone and expensive.

    Solution through reflection (https://play.golang.org/p/3b5I77QMsFI):

    type command struct {
        name    string
        handler reflect.Value
        args    []reflect.Value
    }
    
    var table = map[string]command{
        "Foo": {
            name:    "Foo",
            handler: reflect.ValueOf(foo),
            args: []reflect.Value{
                reflect.ValueOf(1),
                reflect.ValueOf("hello"),
            },
        },
        "Bar": {
            name:    "Bar",
            handler: reflect.ValueOf(bar),
            args: []reflect.Value{
                reflect.ValueOf(true),
                reflect.ValueOf(5),
                reflect.ValueOf("hello"),
            },
        },
    }
    
    func foo(f1 int, f2 string) {
        fmt.Println("Foo:", f1, f2)
    }
    func bar(b1 bool, b2 int, b3 string) {
        fmt.Println("Bar:", b1, b2, b3)
    }
    
    func main() {
        for name, command := range table {
            fmt.Println("Running", name)
            command.handler.Call(command.args)
        }
    }
    

    Solution through function closures (https://play.golang.org/p/8fM86lxalq1):

    type MyFuncType func()
    
    type command struct {
        name    string
        handler MyFuncType
    }
    
    var table = map[string]command{
        "Foo": {
            name:    "Foo",
            handler: fooClosure(1, "hello"),
        },
        "Bar": {
            name:    "Bar",
            handler: barClosure(true, 5, "hello"),
        },
    }
    
    func foo(f1 int, f2 string) {
        fmt.Println("Foo:", f1, f2)
    }
    
    func fooClosure(f1 int, f2 string) MyFuncType {
        return func() {
            foo(f1, f2)
        }
    }
    
    func bar(b1 bool, b2 int, b3 string) {
        fmt.Println("Bar:", b1, b2, b3)
    }
    
    func barClosure(b1 bool, b2 int, b3 string) MyFuncType {
        return func() {
            bar(b1, b2, b3)
        }
    }
    
    func main() {
        for name, command := range table {
            fmt.Println("Running", name)
            command.handler()
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?