Skip to content

Instantly share code, notes, and snippets.

@nihtalak
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save nihtalak/54a43d72c6e132c2a333 to your computer and use it in GitHub Desktop.

Select an option

Save nihtalak/54a43d72c6e132c2a333 to your computer and use it in GitHub Desktop.
Injected context
struct Context {
db *sql.DB
}
struct AppHandler {
context *Context
hanlder func(response http.ResponseWriter, request *http.Request, _ httprouter.Params)
}
func (ah AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ah.handler(ah.context, w, r)
}
func main() {
context = &InitContext()
router.GET("/", AppHandler{context, Handler})
}
func Handler(context *Context, w http.ResponseWriter, r *http.Request) (int, error) {
// access to db via
context.db.Query()
}
@nihtalak
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment