I am trying to create middleware inside routes and wondering how one can get the values of arguments passed inside a func() argument.
For example:
func (c appContainer) Get(path string, fn func(rw http.ResponseWriter, req *http.Request)) {
// HOW DO I GET THE VALUES OF rw AND req PASSED IN fn func()?
c.providers[ROUTER].(Routable).Get(path, fn)
}
I looked through the reflection docs but it's not clear to me or perhaps there is a simpler way?
EDITED (SOLUTION)
It turns out reflection is not needed, as suggested by Adam in his response to this post, as well as Jason on his golang-nuts reply to my question.
The idea is to create a new anonymous function which then intercepts the parameters passed to it for modification/enhancement before calling the original function.
This is what I ended up doing and it worked like a charm, which I am posting in case it helps someone else:
type handlerFn func(rw http.ResponseWriter, req *http.Request)
func (c appContainer) Get(path string, fn handlerFn) {
nfn := func(rw http.ResponseWriter, req *http.Request) {
c.providers[LOGGER].(Loggable).Info("[%s] %s", req.Method, req.URL.Path)
fn(rw, req)
}
c.providers[ROUTER].(Routable).Get(path, nfn)
}