golang操作函数中有一些方法,比如在C ++中使用std :: bind吗?

时间:2017-05-02 03:17:22

标签: c++ go

这是我的设计:

type Handler func(c *gin.Context)
func PreExecute(c *gin.Context, handle_func Handler) Handler {
    if c.User.IsAuthorized {
        return handle_func
    } else {
        return some_error_handle_func
    }
}

我希望通过PreExecute来装饰每个处理程序,就像Python中的装饰一样。所以我在golang中寻找一些std :: bind函数来使PreExecute与Handler完全相同的签名

我的期望:

handler = std::bind(PreExecute, _1, handler) // where _1 hold the position for parameter `c *gin.Context`

某些函数,如std :: bind

我怎么能在Golang中这样做?有没有更优雅的方法来实现这项工作?

1 个答案:

答案 0 :(得分:2)

关闭,这是我的解决方案:

handler_new = func(c *gin.Context){ return PreExecute(c, handler_old)(c) } // since there is no return value of handler, so just do not use `return` statements, I will not correct this error

然后handler_new可以用作:

handler_new(some_context)

相同
handler_new = std::bind(PreExecute, _1, handler_old)

在C ++中

相关问题