Die einzige andere Lösung, die ich gesehen habe, zum Beispiel in "Passing Context to Interface Methods", ist:
Erstellen Sie eine struct die einen eingebetteten Kontext und unseren handler akzeptiert type, und wir erfüllen immer noch den http.Handler Schnittstelle dank ServeHTTP .
In Ihrem Fall die struct würde den pool enthalten , und der handler Funktion.
type appContext struct {
pool Pool
}
type appHandler struct {
*appContext
h func(a *appContext, w http.ResponseWriter, r *http.Request) (int, error)
}
func (ah appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
}
func main() {
context := &appContext{
pool: ...,
// any other data
}
}