Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created June 22, 2011 17:07
Show Gist options
  • Save kylelemons/1040570 to your computer and use it in GitHub Desktop.
Save kylelemons/1040570 to your computer and use it in GitHub Desktop.
How to pass "extra arguments" to handlers in Go
type HandlerData struct {
data string
//whatever
}
func main(){
//Create a data struct
// add data to struct
h := &HandlerData{...}
http.Handle("/foo", http.HandlerFunc(h.Handle))
}
func (h *HandlerData) Handle(w. http.ResponseWriter, req *http.Request){
//data that was initilized here to be used...
w.Write(h.data) //Data has been json and marshalled
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment