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.


Server


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

Handler


func SomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Context().Value(CtxSomeKey))

// ....
}