From 1edfb4cc7592120e571c7f2e8fd2ade038fb6481 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 29 Sep 2010 11:12:52 -0700 Subject: [PATCH] Effective Go: update for new http interface. R=rsc, stephenm CC=golang-dev https://golang.org/cl/2310041 --- doc/effective_go.html | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/doc/effective_go.html b/doc/effective_go.html index a04152e49c..fa888b97db 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -1854,10 +1854,18 @@ that implements Handler can serve HTTP requests.

 type Handler interface {
-    ServeHTTP(*Conn, *Request)
+    ServeHTTP(ResponseWriter, *Request)
 }
 

+ResponseWriter is itself an interface that provides access +to the methods needed to return the response to the client. +Those methods include the standard Write method, so an +http.ResponseWriter can be used wherever an io.Writer +can be used. +Request is a struct containing a parsed representation +of the request from the client. +

For brevity, let's ignore POSTs and assume HTTP requests are always GETs; that simplification does not affect the way the handlers are set up. Here's a trivial but complete implementation of a handler to @@ -1870,13 +1878,14 @@ type Counter struct { n int } -func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) { +func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) { ctr.n++ - fmt.Fprintf(c, "counter = %d\n", ctr.n) + fmt.Fprintf(w, "counter = %d\n", ctr.n) }

-(Keeping with our theme, note how Fprintf can print to an HTTP connection.) +(Keeping with our theme, note how Fprintf can print to an +http.ResponseWriter.) For reference, here's how to attach such a server to a node on the URL tree.

 import "http"
@@ -1892,9 +1901,9 @@ But why make Counter a struct?  An integer is all that's needed.
 // Simpler counter server.
 type Counter int
 
-func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
+func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
     *ctr++
-    fmt.Fprintf(c, "counter = %d\n", *ctr)
+    fmt.Fprintf(w, "counter = %d\n", *ctr)
 }
 

@@ -1906,9 +1915,9 @@ has been visited? Tie a channel to the web page. // (Probably want the channel to be buffered.) type Chan chan *http.Request -func (ch Chan) ServeHTTP(c *http.Conn, req *http.Request) { +func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) { ch <- req - fmt.Fprint(c, "notification sent") + fmt.Fprint(w, "notification sent") }

@@ -1935,11 +1944,11 @@ The http package contains this code: // ordinary functions as HTTP handlers. If f is a function // with the appropriate signature, HandlerFunc(f) is a // Handler object that calls f. -type HandlerFunc func(*Conn, *Request) +type HandlerFunc func(ResponseWriter, *Request) // ServeHTTP calls f(c, req). -func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) { - f(c, req) +func (f HandlerFunc) ServeHTTP(w ResponseWriter, req *Request) { + f(w, req) }

@@ -1955,9 +1964,9 @@ to have the right signature.

 // Argument server.
-func ArgServer(c *http.Conn, req *http.Request) {
+func ArgServer(w http.ResponseWriter, req *http.Request) {
     for i, s := range os.Args {
-        fmt.Fprintln(c, s)
+        fmt.Fprintln(w, s)
     }
 }
 
@@ -2794,8 +2803,8 @@ func main() { } } -func QR(c *http.Conn, req *http.Request) { - templ.Execute(req.FormValue("s"), c) +func QR(w http.ResponseWriter, req *http.Request) { + templ.Execute(req.FormValue("s"), w) } func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {