FAQ
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?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • David Symonds at Jul 2, 2015 at 5:46 am
    There isn't a way. A function value doesn't come with its arguments.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jason Woods at Jul 2, 2015 at 7:08 am

    On 2 Jul 2015, at 05:04, Ricardo Rossi wrote:



    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?

    The function passed as a parameter hasn't been called yet so you can't. Presumedly the Get() calls it somewhere down the line.

    One way could be to create a new anonymous function which then intercepts the parameters passed to it for modification/enhancement before calling the original.

    Then pass the new func to Get()

    Your func will get called instead so you can do what you need to do to the parameters before calling the original callback.

    http://play.golang.org/p/uU_EGthNC5

    Jason

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ricardo Rossi at Jul 2, 2015 at 3:36 pm
    Thanks Jason!


    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)}

    On Thursday, July 2, 2015 at 2:09:01 AM UTC-5, Driskell wrote:


    On 2 Jul 2015, at 05:04, Ricardo Rossi <endat...@gmail.com <javascript:>>
    wrote:


    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?



    The function passed as a parameter hasn't been called yet so you can't.
    Presumedly the Get() calls it somewhere down the line.

    One way could be to create a new anonymous function which then intercepts
    the parameters passed to it for modification/enhancement before calling the
    original.

    Then pass the new func to Get()

    Your func will get called instead so you can do what you need to do to the
    parameters before calling the original callback.

    http://play.golang.org/p/uU_EGthNC5

    Jason
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 2, '15 at 5:37a
activeJul 2, '15 at 3:36p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase