You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

value.go 436B

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