mirror of
https://github.com/golang/go
synced 2024-11-21 19:54:41 -07:00
Effective Go: update for new http interface.
R=rsc, stephenm CC=golang-dev https://golang.org/cl/2310041
This commit is contained in:
parent
4164d60cc2
commit
1edfb4cc75
@ -1854,10 +1854,18 @@ that implements <code>Handler</code> can serve HTTP requests.
|
|||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
ServeHTTP(*Conn, *Request)
|
ServeHTTP(ResponseWriter, *Request)
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
|
<code>ResponseWriter</code> is itself an interface that provides access
|
||||||
|
to the methods needed to return the response to the client.
|
||||||
|
Those methods include the standard <code>Write</code> method, so an
|
||||||
|
<code>http.ResponseWriter</code> can be used wherever an <code>io.Writer</code>
|
||||||
|
can be used.
|
||||||
|
<code>Request</code> is a struct containing a parsed representation
|
||||||
|
of the request from the client.
|
||||||
|
<p>
|
||||||
For brevity, let's ignore POSTs and assume HTTP requests are always
|
For brevity, let's ignore POSTs and assume HTTP requests are always
|
||||||
GETs; that simplification does not affect the way the handlers are
|
GETs; that simplification does not affect the way the handlers are
|
||||||
set up. Here's a trivial but complete implementation of a handler to
|
set up. Here's a trivial but complete implementation of a handler to
|
||||||
@ -1870,13 +1878,14 @@ type Counter struct {
|
|||||||
n int
|
n int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
|
func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
ctr.n++
|
ctr.n++
|
||||||
fmt.Fprintf(c, "counter = %d\n", ctr.n)
|
fmt.Fprintf(w, "counter = %d\n", ctr.n)
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
(Keeping with our theme, note how <code>Fprintf</code> can print to an HTTP connection.)
|
(Keeping with our theme, note how <code>Fprintf</code> can print to an
|
||||||
|
<code>http.ResponseWriter</code>.)
|
||||||
For reference, here's how to attach such a server to a node on the URL tree.
|
For reference, here's how to attach such a server to a node on the URL tree.
|
||||||
<pre>
|
<pre>
|
||||||
import "http"
|
import "http"
|
||||||
@ -1892,9 +1901,9 @@ But why make <code>Counter</code> a struct? An integer is all that's needed.
|
|||||||
// Simpler counter server.
|
// Simpler counter server.
|
||||||
type Counter int
|
type Counter int
|
||||||
|
|
||||||
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
|
func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
*ctr++
|
*ctr++
|
||||||
fmt.Fprintf(c, "counter = %d\n", *ctr)
|
fmt.Fprintf(w, "counter = %d\n", *ctr)
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
@ -1906,9 +1915,9 @@ has been visited? Tie a channel to the web page.
|
|||||||
// (Probably want the channel to be buffered.)
|
// (Probably want the channel to be buffered.)
|
||||||
type Chan chan *http.Request
|
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
|
ch <- req
|
||||||
fmt.Fprint(c, "notification sent")
|
fmt.Fprint(w, "notification sent")
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
@ -1935,11 +1944,11 @@ The <code>http</code> package contains this code:
|
|||||||
// ordinary functions as HTTP handlers. If f is a function
|
// ordinary functions as HTTP handlers. If f is a function
|
||||||
// with the appropriate signature, HandlerFunc(f) is a
|
// with the appropriate signature, HandlerFunc(f) is a
|
||||||
// Handler object that calls f.
|
// Handler object that calls f.
|
||||||
type HandlerFunc func(*Conn, *Request)
|
type HandlerFunc func(ResponseWriter, *Request)
|
||||||
|
|
||||||
// ServeHTTP calls f(c, req).
|
// ServeHTTP calls f(c, req).
|
||||||
func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
|
func (f HandlerFunc) ServeHTTP(w ResponseWriter, req *Request) {
|
||||||
f(c, req)
|
f(w, req)
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
@ -1955,9 +1964,9 @@ to have the right signature.
|
|||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
// Argument server.
|
// Argument server.
|
||||||
func ArgServer(c *http.Conn, req *http.Request) {
|
func ArgServer(w http.ResponseWriter, req *http.Request) {
|
||||||
for i, s := range os.Args {
|
for i, s := range os.Args {
|
||||||
fmt.Fprintln(c, s)
|
fmt.Fprintln(w, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
@ -2794,8 +2803,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func QR(c *http.Conn, req *http.Request) {
|
func QR(w http.ResponseWriter, req *http.Request) {
|
||||||
templ.Execute(req.FormValue("s"), c)
|
templ.Execute(req.FormValue("s"), w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
|
func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user