Created
June 22, 2011 17:07
-
-
Save kylelemons/1040570 to your computer and use it in GitHub Desktop.
How to pass "extra arguments" to handlers in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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