Jedyne inne rozwiązanie, jakie widziałem, na przykład w „Przekazywanie kontekstu do metod interfejsu” to:
utwórz struct
który akceptuje osadzony kontekst i naszą handler
i nadal spełniamy wymagania http.Handler
interfejs dzięki ServeHTTP
.
W twoim przypadku struct
obejmowałoby pool
i handler
funkcja.
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
}
}