gorilla/context

Wrong use of gorilla/context ?

hackintoshrao opened this issue · 2 comments

The website https://www.nicolasmerouze.com/share-values-between-middlewares-context-golang/ , defines the use of gorilla in the following manner ,

func myHandler(w http.ResponseWriter, r *http.Request) {
      context.Set(r, "foo", "bar")
}

func myOtherHandler(w http.ResponseWriter, r *http.Request) {
  val := context.Get(r, "foo").(string)
}

Is it possible to do a Set in one handler and then Get in another handler to fetch back the former ?Its not possible right ?

No: you can't set/get across different requests as *http.Request is the
implicit map key.

You can do it across handlers if they are chained - i.e. one is acting as
middleware, wrapping the other.
On Tue, 1 Sep 2015 at 5:55 pm karthic rao notifications@github.com wrote:

The website
https://www.nicolasmerouze.com/share-values-between-middlewares-context-golang/
, defines the use of gorilla in the following manner ,

func myHandler(w http.ResponseWriter, r *http.Request) {
context.Set(r, "foo", "bar")
}

func myOtherHandler(w http.ResponseWriter, r *http.Request) {
val := context.Get(r, "foo").(string)
}

Is it possible to do a Set in one handler and then Get in another handler
to fetch back the former ?


Reply to this email directly or view it on GitHub
#25.

Yes , Thank you