Hacker News new | ask | show | jobs
by angra_mainyu 866 days ago
I just use a closure. So instead of defining a handler as:

  func HandleX(w http.ResponseWriter, req *http.Request) {
    // code
  }
I use:

  func HandleX(store *DataStore, dep1 Foo, dep2 Bar, commonDep Common) http.HandlerFunc {
    //
    // maybe some initialization
    //
    return func(w http.ResponseWriter, req *http.Request) {
      // code
    }
  }
and initialize them all once in whatever entrypoint there is.