title: Wrapping a handler with a middleware that has parameters with Go-chi
body: <p>i've been trying to implement <a href="https://medium.com/@matryer/the-http-handler-wrapper-technique-in-golang-updated-bc7fbcffa702" rel="nofollow noreferrer">this</a> tutorial with go-chi and especially the part about wrapping / passing an argument to a wrapper.</p>
  <p>My goal is to be able to wrap some specific routes with a middleware with custom parameters for that route instead of having middleware that are &quot; global &quot; to all my routes but i'm having issues doing it.</p>
  <pre><code>package main
  
  import (
      &quot;context&quot;
      &quot;io&quot;
      &quot;net/http&quot;
  
      &quot;github.com/go-chi/chi&quot;
      &quot;github.com/go-chi/chi/middleware&quot;
  )
  
  func main() {
  
      r := chi.NewRouter()
      r.Use(middleware.Logger)
  
      r.Get(&quot;/user&quot;, MustParams(sayHello, &quot;key&quot;, &quot;auth&quot;))
  
      http.ListenAndServe(&quot;:3000&quot;, r)
  }
  
  
  func sayHello(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte(&quot;hi&quot;))
  }
  
  func MustParams(h http.Handler, params ...string) http.Handler {
      return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  
          q := r.URL.Query()
          for _, param := range params {
              if len(q.Get(param)) == 0 {
                  http.Error(w, &quot;missing &quot;+param, http.StatusBadRequest)
                  return // exit early
              }
          }
          h.ServeHTTP(w, r) // all params present, proceed
      })
  }
  </code></pre>
  <p>i'm getting <code>cannot use sayHello (type func(http.ResponseWriter, *http.Request)) as type http.Handler in argument to MustParams: func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method)</code></p>
  <p>and if i try to type assert it doing <code>r.Get(&quot;/user&quot;, MustParams(http.HandleFunc(sayHello), &quot;key&quot;, &quot;auth&quot;))</code></p>
  <p>i get the error <code>cannot use MustParams(http.HandleFunc(sayHello), &quot;key&quot;, &quot;auth&quot;) (type http.Handler) as type http.HandlerFunc in argument to r.Get: need type assertion</code></p>
  <p>I can't seem to find a way to make it work or to be able to wrapping a single route with a middleware.</p>