選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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