Commit f95a11e2 authored by Rob Pike's avatar Rob Pike

further pedagogy: a channel that satisfies the HTTP server interface

R=rsc
DELTA=18  (18 added, 0 deleted, 0 changed)
OCL=24482
CL=24484
parent b0009bef
...@@ -45,11 +45,29 @@ func FileServer(c *http.Conn, req *http.Request) { ...@@ -45,11 +45,29 @@ func FileServer(c *http.Conn, req *http.Request) {
fmt.Fprintf(c, "[%d bytes]\n", n); fmt.Fprintf(c, "[%d bytes]\n", n);
} }
// a channel (just for the fun of it)
type Chan chan int
func ChanCreate() Chan {
c := make(Chan);
go func(c Chan) {
for x := 0;; x++ {
c <- x
}
}(c);
return c;
}
func (ch Chan) ServeHTTP(c *http.Conn, req *http.Request) {
io.WriteString(c, fmt.Sprintf("channel send #%d\n", <-ch));
}
func main() { func main() {
flag.Parse(); flag.Parse();
http.Handle("/counter", new(Counter)); http.Handle("/counter", new(Counter));
http.Handle("/go/", http.HandlerFunc(FileServer)); http.Handle("/go/", http.HandlerFunc(FileServer));
http.Handle("/go/hello", http.HandlerFunc(HelloServer)); http.Handle("/go/hello", http.HandlerFunc(HelloServer));
http.Handle("/chan", ChanCreate());
err := http.ListenAndServe(":12345", nil); err := http.ListenAndServe(":12345", nil);
if err != nil { if err != nil {
panic("ListenAndServe: ", err.String()) panic("ListenAndServe: ", err.String())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment