03/03/2020 - GO
If you wish to add set of key and value pair to HTTP server's context then fetch them through Request context, you can use example below.
type ctxSome int
const CtxSomeKey = ctxSome(iota)
ctx := context.WithValue(context.Background(), CtxSomeKey, "some-value")
srv := &http.Server{
Addr: address,
Handler: handler,
BaseContext: func(listener net.Listener) context.Context {
return ctx
},
}
func SomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Context().Value(CtxSomeKey))
// ....
}