value.go 390 B

1234567891011121314151617
  1. package middleware
  2. import (
  3. "context"
  4. "net/http"
  5. )
  6. // WithValue set k/v in a context chain.
  7. func WithValue(k, v interface{}) func(next http.Handler) http.Handler {
  8. return func(next http.Handler) http.Handler {
  9. f := func(w http.ResponseWriter, r *http.Request) {
  10. r = r.WithContext(context.WithValue(r.Context(), k, v))
  11. next.ServeHTTP(w, r)
  12. }
  13. return http.HandlerFunc(f)
  14. }
  15. }