
Founder/CEO of @bigfrankrecords. Freelance Music Producer, Mixing, Composer. Founder/Trainer of @gopherguides.
- Tweets22K
- Following155
- Followers6K
- Likes3.8K

ALT sync.Once to defer setup If I have to do anything expensive when preparing the handler, I defer it until that handler is first called. This improves application startup time. Go Copy code to clipboard Copy func handleTemplate(files string...) http.HandlerFunc { var ( init sync.Once tpl *template.Template tplerr error ) return func(w http.ResponseWriter, r *http.Request) { init.Do(func(){ tpl, tplerr = template.ParseFiles(files...) }) if tplerr != nil { http.Error(w, tplerr.Error(), http.StatusInternalServerError) return } // use tpl } } sync.Once ensures the code is only executed one time, and other calls (other people making the same request) will block until it’s finished. The error check is outside of the init function, so if something does go wrong we still surface the error and won’t lose it in the logs If the handler is not called, the expensive work is never done — this can have big benefits depending on how your code is deployed Remember t







