title: How to compose a new ServeMux into a larger struct?
body: <p>In previous golang applications I've made that use the DefaultServeMux, if I had a route like this <code>"/users/"</code> route that was handled by <code>func user(name string)</code>, for example, and I sent a request to <code>/users/jim</code>, the request would still be handled by the <code>"/users/"</code> route. In the current application I'm making, I'm not using <code>DefaultServeMux</code> but merely passing a handler that implements ServeHTTP and then switching on the request url. However, now, if I send a request to a route that doesn't exactly match, the handler function doesn't get called. For example, if I send a post request to <code>"/api/jim"</code>, I get a 404 error even though <code>"/api"</code> is handled.</p>
  
  <p>I want to keep my application the way it is (with a reference to the DB in the handler) but also be able to handle routes that don't match exactly. </p>
  
  <p>Question: Assuming I could create a new ServeMux to handle routes that don't exactly match, but how can I compose that with my type Handler Struct that has the reference to the database connection?</p>
  
  <pre><code>type Handler struct{
    DB *DB
  }
  
  func main() {
      fmt.Println("Hello, playground")
  
  
      db, err := sql.Open("postgres", dbinfo)
      defer db.Close()
  
      h := &amp;Handler{
        DB: db,
      }
  
      log.Fatal(http.ListenAndServe(":8888", h))
  }
  
  func (h *Handler)ServeHTTP(w http.ResponseWriter, r *http.Request){
  
      switch r.URL.Path{
          case "/":
          h.serveRoot(w, r)
          case "/api/":
          h.apiRouter(w, r)
      }
  }
  
  func (h *Handler)serveRoot(w http.ResponseWriter, r *http.Request){
          h.DB.DoSomethingWithDB()
  }
  
  func (h *Handler)apiRouter(w http.ResponseWriter, r *http.Request){
  
         switch r.URL.Path{
  
             case "/":
                 h.serveRoot(w, r)
             case "/api/":
                 h.apiRouter(w, r)
  
         }
  }
  </code></pre>
  
  <p><strong>Update</strong>
  For reasons that aren't relevant to the question, I can't use DefaultServeMux  </p>