Eğer uygulama başlangıcında HTTP sunucusunun context'ine anahtar ve değerler ekleyip, onlara Request context içinden ulaşmak isterseniz, aşağıdaki örneği kullanabiliriz.


Sunucu


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

Request yakalayıcı


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

// ....
}