Write *httptest.ResponseRecorder (which implements http.ResponseWriter ) and check it out.
Example from the package:
package main import ( "fmt" "log" "net/http" "net/http/httptest" ) func main() { handler := func(w http.ResponseWriter, r *http.Request) { http.Error(w, "something failed", http.StatusInternalServerError) } req, err := http.NewRequest("GET", "http://example.com/foo", nil) if err != nil { log.Fatal(err) } w := httptest.NewRecorder() handler(w, req) fmt.Printf("%d - %s", w.Code, w.Body.String()) }
Change the answer to the question in the comments:
If I understand your question correctly, then yes, you can use closure for this.
As a handler, consider the following:
func MyHandler(w http.ResponseWriter, r *http.Request) {
You can then register the following closure with servemux to achieve the desired effect:
http.HandleFunc("/my/url", func(w http.ResponseWriter, r *http.Request) { // first call MyHandler MyHandler(w, r) // then log whatever you need log.Printf("%#v\n", w) })
If this model is useful to you, you can write a higher order method that wraps any func(http.ResponseWriter, *http.Request) in such a closure. This is a topic for myself, though.