reflection - How do I get the values of arguments passed inside a func() argument in GO? -
i trying create middleware inside routes , wondering how 1 can values of arguments passed inside func() argument.
for example:
func (c appcontainer) get(path string, fn func(rw http.responsewriter, req *http.request)) { // how values of rw , req passed in fn func()? c.providers[router].(routable).get(path, fn) }
i looked through reflection docs it's not clear me or perhaps there simpler way?
edited (solution)
it turns out reflection not needed, suggested adam in response post, jason on his golang-nuts reply question.
the idea create new anonymous function intercepts parameters passed modification/enhancement before calling original function.
this ended doing , worked charm, posting in case helps 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) }
simple answer: don't. not @ place @ least
the variables rw
, req
make first sense if function fn
called. (or function calls fn, have rw
, req
variables)
in case appcontainer
uses routes configured
to better understand how middleware concept works simple example can found here: https://golang.org/doc/articles/wiki/
you might want scroll down "introducing function literals , closures"
Comments
Post a Comment