Here is my design:
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
}
}
and I want to decorate every handler by PreExecute just like the decoration in Python. So I am looking for some std::bind functions in golang to get PreExecute a exactly same signature as Handler
What I expect:
handler = std::bind(PreExecute, _1, handler) // where _1 hold the position for parameter `c *gin.Context`
some function like std::bind
How can I do that in Golang? Are there some more elegant methods to get this work?
Closure, here is my solution:
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
and then handler_new can be used as:
handler_new(some_context)
It's the same as
handler_new = std::bind(PreExecute, _1, handler_old)
in C++