You can place the dependencies into a struct and use a bound method on that as your handler. So you build a struct that will hold application-scoped objects like Redis client, content.Updater, etc.
package contact type Handler struct { Redis *redis.Client Updater *content.Updater Mapper *mapping.Updater } func (h *Handler) Endpoint1(w http.ResponseWriter, r *http.Request) { // ... } Then build the Handler when you’re setting up the router (presumably in main() or nearby) and register it against the router as usual. handler := contact.Handler{ Redis: config.GetRedisDb(), Updater: contents.GetUpdater(), Mapper: mapping.GetUpdater(), } myRouter.Handle("/endpoint1/", http.HandlerFunc(h.Endpoint1)).Methods("POST", "OPTIONS") // or myRouter.HandleFunc("/endpoint1/", h.Endpoint1).Methods("POST", "OPTIONS") On Mon, Sep 23, 2019 at 2:37 AM Nathanael Curin <n.cu...@capitaldata.fr> wrote: > I'm avoiding context.WithValue since I feel the objects I'm dealing with > are too "powerful" (i.e. not simple Values). For a read, a 2016 article on > this : https://peter.bourgon.org/blog/2016/07/11/context.html > > <https://urldefense.proofpoint.com/v2/url?u=https-3A__peter.bourgon.org_blog_2016_07_11_context.html&d=DwMFaQ&c=r2dcLCtU9q6n0vrtnDw9vg&r=1l8pq-0AmiqF9VqB_zc2sA&m=NI61KpC9jRpLlUlKWVRDZmipntercznF0NBSCnIuTxo&s=pE-cXfAKqzEre0LV5lcGQmX_oGtu8kIyJyU7M14ZwWs&e=> > I remember reading through another article saying the usage of WithValue > is kind of risky when time goes on, since it's just a magical interface{} > store. Performance-wise I'm not sure though. > > Though, you're right, that'd be another way to do it. Waiting for people > to chime in with their ideas too (hopefully!) :) > > > Le lundi 23 septembre 2019 11:20:52 UTC+2, Andrew Pillar a écrit : >> >> Why don't you use context.WithValue(r.Context(), <key>, <value>) for >> passing >> the injected values you want to your HTTP handlers. Then defer the logic >> for >> retrieving whatever you want for that handler to a middleware function. >> > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/c23761e8-1287-4758-8303-b39db8cf22f0%40googlegroups.com > <https://urldefense.proofpoint.com/v2/url?u=https-3A__groups.google.com_d_msgid_golang-2Dnuts_c23761e8-2D1287-2D4758-2D8303-2Db39db8cf22f0-2540googlegroups.com-3Futm-5Fmedium-3Demail-26utm-5Fsource-3Dfooter&d=DwMFaQ&c=r2dcLCtU9q6n0vrtnDw9vg&r=1l8pq-0AmiqF9VqB_zc2sA&m=NI61KpC9jRpLlUlKWVRDZmipntercznF0NBSCnIuTxo&s=ZNX_GIIPz98YRMi2pEBylgj0avplFYR0Sm9gopwCNeM&e=> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAGeKYdXhUsfE0_ETFTCKKU1kJyY2zqfWJ8HeikaiYoC8%2B05u2Q%40mail.gmail.com.