google app engine - Appengine with Go: Is there a http.Handle prehook or something similar? -


suppose have following init function routing requests.

func init() {     http.handlefunc("/user", handler1)     http.handlefunc("/user/profile", handler2)     http.handlefunc("/user/post", handler3)     ....     .... } 

all of these require have user's profile.

i know can

func handler1(w http.responsewriter, r *http.request) {     getuserdata()     //actual handler code     ...     ... } 

but, there way can data without putting function call in every handler? go want in first place?

you have 2 options.

  1. you can inplement http.handler interface
  2. you wrap http.handlerfunc wrapper handlefunc.

since looks want simple i'll illustrate wrapper

func prehook(f http.handlerfunc) http.handlerfunc {   return func(w http.responsewriter, r *http.request) {     getuserdata()     f(w, r)   } }  func init() {     // use getuserdata() call before handler     http.handlefunc("/user", prehook(handler1))     // don't use getuserdata call before handler     http.handlefunc("/user/profile", handler2) } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -