From 3e45412327a2654a77944249962b3652e6142299 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Mon, 17 Jan 2011 12:40:45 +0100 Subject: Imported Upstream version 2011.01.12 --- doc/codelab/wiki/Makefile | 15 ++-- doc/codelab/wiki/final-noclosure.go | 34 ++++---- doc/codelab/wiki/final-noerror.go | 8 +- doc/codelab/wiki/final-parsetemplate.go | 32 +++---- doc/codelab/wiki/final-template.go | 16 ++-- doc/codelab/wiki/final.go | 30 +++---- doc/codelab/wiki/http-sample.go | 4 +- doc/codelab/wiki/index.html | 147 +++++++++++++++----------------- doc/codelab/wiki/notemplate.go | 8 +- doc/codelab/wiki/part2.go | 4 +- doc/codelab/wiki/srcextract.go | 2 +- doc/codelab/wiki/wiki.html | 35 ++++---- 12 files changed, 160 insertions(+), 175 deletions(-) (limited to 'doc/codelab') diff --git a/doc/codelab/wiki/Makefile b/doc/codelab/wiki/Makefile index 76ab5c5bc..e0549fc8e 100644 --- a/doc/codelab/wiki/Makefile +++ b/doc/codelab/wiki/Makefile @@ -2,16 +2,11 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../src/Make.$(GOARCH) +include ../../../src/Make.inc all: index.html -# ugly hack to deal with whitespaces in $GOROOT -nullstring := -space := $(nullstring) # a space at the end -QUOTED_GOROOT:=$(subst $(space),\ ,$(GOROOT)) - -include $(QUOTED_GOROOT)/src/Make.common +include ../../../src/Make.common CLEANFILES+=index.html srcextract.bin htmlify.bin @@ -23,7 +18,7 @@ test: final.bin rm -f final.6 final.bin %.bin: %.$O - $(QUOTED_GOBIN)/$(LD) -o $@ $< -%.$O: - $(QUOTED_GOBIN)/$(GC) $*.go + $(LD) -o $@ $< +%.$O: + $(GC) $*.go diff --git a/doc/codelab/wiki/final-noclosure.go b/doc/codelab/wiki/final-noclosure.go index d4ce71560..2f48565ca 100644 --- a/doc/codelab/wiki/final-noclosure.go +++ b/doc/codelab/wiki/final-noclosure.go @@ -27,21 +27,21 @@ func loadPage(title string) (*page, os.Error) { return &page{title: title, body: body}, nil } -func viewHandler(c *http.Conn, r *http.Request) { - title, err := getTitle(c, r) +func viewHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) if err != nil { return } p, err := loadPage(title) if err != nil { - http.Redirect(c, "/edit/"+title, http.StatusFound) + http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) } -func editHandler(c *http.Conn, r *http.Request) { - title, err := getTitle(c, r) +func editHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) if err != nil { return } @@ -49,11 +49,11 @@ func editHandler(c *http.Conn, r *http.Request) { if err != nil { p = &page{title: title} } - renderTemplate(c, "edit", p) + renderTemplate(w, "edit", p) } -func saveHandler(c *http.Conn, r *http.Request) { - title, err := getTitle(c, r) +func saveHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) if err != nil { return } @@ -61,21 +61,21 @@ func saveHandler(c *http.Conn, r *http.Request) { p := &page{title: title, body: []byte(body)} err = p.save() if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) } -func renderTemplate(c *http.Conn, tmpl string, p *page) { +func renderTemplate(w http.ResponseWriter, tmpl string, p *page) { t, err := template.ParseFile(tmpl+".html", nil) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - err = t.Execute(p, c) + err = t.Execute(p, w) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) } } @@ -83,10 +83,10 @@ const lenPath = len("/view/") var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$") -func getTitle(c *http.Conn, r *http.Request) (title string, err os.Error) { +func getTitle(w http.ResponseWriter, r *http.Request) (title string, err os.Error) { title = r.URL.Path[lenPath:] if !titleValidator.MatchString(title) { - http.NotFound(c, r) + http.NotFound(w, r) err = os.NewError("Invalid Page Title") } return diff --git a/doc/codelab/wiki/final-noerror.go b/doc/codelab/wiki/final-noerror.go index 3b699452a..cf4852265 100644 --- a/doc/codelab/wiki/final-noerror.go +++ b/doc/codelab/wiki/final-noerror.go @@ -28,21 +28,21 @@ func loadPage(title string) (*page, os.Error) { const lenPath = len("/view/") -func editHandler(c *http.Conn, r *http.Request) { +func editHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, err := loadPage(title) if err != nil { p = &page{title: title} } t, _ := template.ParseFile("edit.html", nil) - t.Execute(p, c) + t.Execute(p, w) } -func viewHandler(c *http.Conn, r *http.Request) { +func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) t, _ := template.ParseFile("view.html", nil) - t.Execute(p, c) + t.Execute(p, w) } func main() { diff --git a/doc/codelab/wiki/final-parsetemplate.go b/doc/codelab/wiki/final-parsetemplate.go index 93b956b9d..f02d116b2 100644 --- a/doc/codelab/wiki/final-parsetemplate.go +++ b/doc/codelab/wiki/final-parsetemplate.go @@ -27,43 +27,43 @@ func loadPage(title string) (*page, os.Error) { return &page{title: title, body: body}, nil } -func viewHandler(c *http.Conn, r *http.Request, title string) { +func viewHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { - http.Redirect(c, "/edit/"+title, http.StatusFound) + http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) } -func editHandler(c *http.Conn, r *http.Request, title string) { +func editHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { p = &page{title: title} } - renderTemplate(c, "edit", p) + renderTemplate(w, "edit", p) } -func saveHandler(c *http.Conn, r *http.Request, title string) { +func saveHandler(w http.ResponseWriter, r *http.Request, title string) { body := r.FormValue("body") p := &page{title: title, body: []byte(body)} err := p.save() if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) } -func renderTemplate(c *http.Conn, tmpl string, p *page) { +func renderTemplate(w http.ResponseWriter, tmpl string, p *page) { t, err := template.ParseFile(tmpl+".html", nil) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - err = t.Execute(p, c) + err = t.Execute(p, w) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) } } @@ -71,14 +71,14 @@ const lenPath = len("/view/") var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$") -func makeHandler(fn func(*http.Conn, *http.Request, string)) http.HandlerFunc { - return func(c *http.Conn, r *http.Request) { +func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] if !titleValidator.MatchString(title) { - http.NotFound(c, r) + http.NotFound(w, r) return } - fn(c, r, title) + fn(w, r, title) } } diff --git a/doc/codelab/wiki/final-template.go b/doc/codelab/wiki/final-template.go index 06c9366ad..0bb133d3a 100644 --- a/doc/codelab/wiki/final-template.go +++ b/doc/codelab/wiki/final-template.go @@ -28,32 +28,32 @@ func loadPage(title string) (*page, os.Error) { const lenPath = len("/view/") -func editHandler(c *http.Conn, r *http.Request) { +func editHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, err := loadPage(title) if err != nil { p = &page{title: title} } - renderTemplate(c, "edit", p) + renderTemplate(w, "edit", p) } -func viewHandler(c *http.Conn, r *http.Request) { +func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) } -func saveHandler(c *http.Conn, r *http.Request) { +func saveHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] body := r.FormValue("body") p := &page{title: title, body: []byte(body)} p.save() - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) } -func renderTemplate(c *http.Conn, tmpl string, p *page) { +func renderTemplate(w http.ResponseWriter, tmpl string, p *page) { t, _ := template.ParseFile(tmpl+".html", nil) - t.Execute(p, c) + t.Execute(p, w) } func main() { diff --git a/doc/codelab/wiki/final.go b/doc/codelab/wiki/final.go index 0186729c2..0c0206bc0 100644 --- a/doc/codelab/wiki/final.go +++ b/doc/codelab/wiki/final.go @@ -27,32 +27,32 @@ func loadPage(title string) (*page, os.Error) { return &page{title: title, body: body}, nil } -func viewHandler(c *http.Conn, r *http.Request, title string) { +func viewHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { - http.Redirect(c, "/edit/"+title, http.StatusFound) + http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } - renderTemplate(c, "view", p) + renderTemplate(w, "view", p) } -func editHandler(c *http.Conn, r *http.Request, title string) { +func editHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { p = &page{title: title} } - renderTemplate(c, "edit", p) + renderTemplate(w, "edit", p) } -func saveHandler(c *http.Conn, r *http.Request, title string) { +func saveHandler(w http.ResponseWriter, r *http.Request, title string) { body := r.FormValue("body") p := &page{title: title, body: []byte(body)} err := p.save() if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) return } - http.Redirect(c, "/view/"+title, http.StatusFound) + http.Redirect(w, r, "/view/"+title, http.StatusFound) } var templates = make(map[string]*template.Template) @@ -63,10 +63,10 @@ func init() { } } -func renderTemplate(c *http.Conn, tmpl string, p *page) { - err := templates[tmpl].Execute(p, c) +func renderTemplate(w http.ResponseWriter, tmpl string, p *page) { + err := templates[tmpl].Execute(p, w) if err != nil { - http.Error(c, err.String(), http.StatusInternalServerError) + http.Error(w, err.String(), http.StatusInternalServerError) } } @@ -74,14 +74,14 @@ const lenPath = len("/view/") var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$") -func makeHandler(fn func(*http.Conn, *http.Request, string)) http.HandlerFunc { - return func(c *http.Conn, r *http.Request) { +func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] if !titleValidator.MatchString(title) { - http.NotFound(c, r) + http.NotFound(w, r) return } - fn(c, r, title) + fn(w, r, title) } } diff --git a/doc/codelab/wiki/http-sample.go b/doc/codelab/wiki/http-sample.go index 11d5d7861..33379a1b6 100644 --- a/doc/codelab/wiki/http-sample.go +++ b/doc/codelab/wiki/http-sample.go @@ -5,8 +5,8 @@ import ( "http" ) -func handler(c *http.Conn, r *http.Request) { - fmt.Fprintf(c, "Hi there, I love %s!", r.URL.Path[1:]) +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { diff --git a/doc/codelab/wiki/index.html b/doc/codelab/wiki/index.html index 107e49f26..c494a3ced 100644 --- a/doc/codelab/wiki/index.html +++ b/doc/codelab/wiki/index.html @@ -1,7 +1,4 @@ -
- -

Writing Web Applications

- +

Introduction

@@ -243,8 +240,8 @@ import ( "http" ) -func handler(c *http.Conn, r *http.Request) { - fmt.Fprintf(c, "Hi there, I love %s!", r.URL.Path[1:]) +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { @@ -269,12 +266,12 @@ This function will block until the program is terminated.

The function handler is of the type http.HandlerFunc. -It takes an http.Conn and http.Request as its -arguments. +It takes an http.ResponseWriter and an http.Request as +its arguments.

-An http.Conn is the server end of an HTTP connection; by writing +An http.ResponseWriter value assembles the HTTP server's response; by writing to it, we send data to the HTTP client.

@@ -317,10 +314,10 @@ Let's create a handler to view a wiki page:
 const lenPath = len("/view/")
 
-func viewHandler(c *http.Conn, r *http.Request) {
+func viewHandler(w http.ResponseWriter, r *http.Request) {
 	title := r.URL.Path[lenPath:]
 	p, _ := loadPage(title)
-	fmt.Fprintf(c, "<h1>%s</h1><div>%s</div>", p.title, p.body)
+	fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.title, p.body)
 }
 
@@ -336,7 +333,7 @@ begin with "/view/", which is not part of the page title.

The function then loads the page data, formats the page with a string of simple -HTML, and writes it to c, the http.Conn. +HTML, and writes it to w, the http.ResponseWriter.

@@ -409,13 +406,13 @@ and displays an HTML form.

-func editHandler(c *http.Conn, r *http.Request) {
+func editHandler(w http.ResponseWriter, r *http.Request) {
 	title := r.URL.Path[lenPath:]
 	p, err := loadPage(title)
 	if err != nil {
 		p = &page{title: title}
 	}
-	fmt.Fprintf(c, "<h1>Editing %s</h1>"+
+	fmt.Fprintf(w, "<h1>Editing %s</h1>"+
 		"<form action=\"/save/%s\" method=\"POST\">"+
 		"<textarea name=\"body\">%s</textarea><br>"+
 		"<input type=\"submit\" value=\"Save\">"+
@@ -471,14 +468,14 @@ HTML:
 

-func editHandler(c *http.Conn, r *http.Request) {
+func editHandler(w http.ResponseWriter, r *http.Request) {
 	title := r.URL.Path[lenPath:]
 	p, err := loadPage(title)
 	if err != nil {
 		p = &page{title: title}
 	}
 	t, _ := template.ParseFile("edit.html", nil)
-	t.Execute(p, c)
+	t.Execute(p, w)
 }
 
@@ -491,7 +488,7 @@ The function template.ParseFile will read the contents of The method t.Execute replaces all occurrences of {title} and {body} with the values of p.title and p.body, and writes the resultant -HTML to the http.Conn. +HTML to the http.ResponseWriter.

@@ -526,11 +523,11 @@ Modify viewHandler accordingly:

-func viewHandler(c *http.Conn, r *http.Request) {
+func viewHandler(w http.ResponseWriter, r *http.Request) {
 	title := r.URL.Path[lenPath:]
 	p, _ := loadPage(title)
 	t, _ := template.ParseFile("view.html", nil)
-	t.Execute(p, c)
+	t.Execute(p, w)
 }
 
@@ -541,24 +538,24 @@ to its own function:

-func viewHandler(c *http.Conn, r *http.Request) {
+func viewHandler(w http.ResponseWriter, r *http.Request) {
 	title := r.URL.Path[lenPath:]
 	p, _ := loadPage(title)
-	renderTemplate(c, "view", p)
+	renderTemplate(w, "view", p)
 }
 
-func editHandler(c *http.Conn, r *http.Request) {
+func editHandler(w http.ResponseWriter, r *http.Request) {
 	title := r.URL.Path[lenPath:]
 	p, err := loadPage(title)
 	if err != nil {
 		p = &page{title: title}
 	}
-	renderTemplate(c, "edit", p)
+	renderTemplate(w, "edit", p)
 }
 
-func renderTemplate(c *http.Conn, tmpl string, p *page) {
+func renderTemplate(w http.ResponseWriter, tmpl string, p *page) {
 	t, _ := template.ParseFile(tmpl+".html", nil)
-	t.Execute(p, c)
+	t.Execute(p, w)
 }
 
@@ -576,13 +573,13 @@ redirect the client to the edit page so the content may be created:

-func viewHandler(c *http.Conn, r *http.Request, title string) {
+func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
 	p, err := loadPage(title)
 	if err != nil {
-		http.Redirect(c, "/edit/"+title, http.StatusFound)
+		http.Redirect(w, r, "/edit/"+title, http.StatusFound)
 		return
 	}
-	renderTemplate(c, "view", p)
+	renderTemplate(w, "view", p)
 }
 
@@ -599,12 +596,12 @@ The function saveHandler will handle the form submission.

-func saveHandler(c *http.Conn, r *http.Request) {
+func saveHandler(w http.ResponseWriter, r *http.Request) {
 	title := r.URL.Path[lenPath:]
 	body := r.FormValue("body")
 	p := &page{title: title, body: []byte(body)}
 	p.save()
-	http.Redirect(c, "/view/"+title, http.StatusFound)
+	http.Redirect(w, r, "/view/"+title, http.StatusFound)
 }
 
@@ -637,15 +634,15 @@ First, let's handle the errors in renderTemplate:

-func renderTemplate(c *http.Conn, tmpl string, p *page) {
+func renderTemplate(w http.ResponseWriter, tmpl string, p *page) {
 	t, err := template.ParseFile(tmpl+".html", nil)
 	if err != nil {
-		http.Error(c, err.String(), http.StatusInternalServerError)
+		http.Error(w, err.String(), http.StatusInternalServerError)
 		return
 	}
-	err = t.Execute(p, c)
+	err = t.Execute(p, w)
 	if err != nil {
-		http.Error(c, err.String(), http.StatusInternalServerError)
+		http.Error(w, err.String(), http.StatusInternalServerError)
 	}
 }
 
@@ -661,15 +658,15 @@ Now let's fix up saveHandler:

-func saveHandler(c *http.Conn, r *http.Request, title string) {
+func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
 	body := r.FormValue("body")
 	p := &page{title: title, body: []byte(body)}
 	err := p.save()
 	if err != nil {
-		http.Error(c, err.String(), http.StatusInternalServerError)
+		http.Error(w, err.String(), http.StatusInternalServerError)
 		return
 	}
-	http.Redirect(c, "/view/"+title, http.StatusFound)
+	http.Redirect(w, r, "/view/"+title, http.StatusFound)
 }
 
@@ -728,10 +725,10 @@ the Execute method on the appropriate Template from templates:
-func renderTemplate(c *http.Conn, tmpl string, p *page) {
-	err := templates[tmpl].Execute(p, c)
+func renderTemplate(w http.ResponseWriter, tmpl string, p *page) {
+	err := templates[tmpl].Execute(p, w)
 	if err != nil {
-		http.Error(c, err.String(), http.StatusInternalServerError)
+		http.Error(w, err.String(), http.StatusInternalServerError)
 	}
 }
 
@@ -768,10 +765,10 @@ URL, and tests it against our titleValidator expression:

-func getTitle(c *http.Conn, r *http.Request) (title string, err os.Error) {
+func getTitle(w http.ResponseWriter, r *http.Request) (title string, err os.Error) {
 	title = r.URL.Path[lenPath:]
 	if !titleValidator.MatchString(title) {
-		http.NotFound(c, r)
+		http.NotFound(w, r)
 		err = os.NewError("Invalid Page Title")
 	}
 	return
@@ -790,21 +787,21 @@ Let's put a call to getTitle in each of the handlers:
 

-func viewHandler(c *http.Conn, r *http.Request) {
-	title, err := getTitle(c, r)
+func viewHandler(w http.ResponseWriter, r *http.Request) {
+	title, err := getTitle(w, r)
 	if err != nil {
 		return
 	}
 	p, err := loadPage(title)
 	if err != nil {
-		http.Redirect(c, "/edit/"+title, http.StatusFound)
+		http.Redirect(w, r, "/edit/"+title, http.StatusFound)
 		return
 	}
-	renderTemplate(c, "view", p)
+	renderTemplate(w, "view", p)
 }
 
-func editHandler(c *http.Conn, r *http.Request) {
-	title, err := getTitle(c, r)
+func editHandler(w http.ResponseWriter, r *http.Request) {
+	title, err := getTitle(w, r)
 	if err != nil {
 		return
 	}
@@ -812,11 +809,11 @@ func editHandler(c *http.Conn, r *http.Request) {
 	if err != nil {
 		p = &page{title: title}
 	}
-	renderTemplate(c, "edit", p)
+	renderTemplate(w, "edit", p)
 }
 
-func saveHandler(c *http.Conn, r *http.Request) {
-	title, err := getTitle(c, r)
+func saveHandler(w http.ResponseWriter, r *http.Request) {
+	title, err := getTitle(w, r)
 	if err != nil {
 		return
 	}
@@ -824,10 +821,10 @@ func saveHandler(c *http.Conn, r *http.Request) {
 	p := &page{title: title, body: []byte(body)}
 	err = p.save()
 	if err != nil {
-		http.Error(c, err.String(), http.StatusInternalServerError)
+		http.Error(w, err.String(), http.StatusInternalServerError)
 		return
 	}
-	http.Redirect(c, "/view/"+title, http.StatusFound)
+	http.Redirect(w, r, "/view/"+title, http.StatusFound)
 }
 
@@ -848,9 +845,9 @@ a title string:

-func viewHandler(c *http.Conn, r *http.Request, title string)
-func editHandler(c *http.Conn, r *http.Request, title string)
-func saveHandler(c *http.Conn, r *http.Request, title string)
+func viewHandler(w http.ResponseWriter, r *http.Request, title string)
+func editHandler(w http.ResponseWriter, r *http.Request, title string)
+func saveHandler(w http.ResponseWriter, r *http.Request, title string)
 

@@ -860,8 +857,8 @@ type, and returns a function of type http.HandlerFunc

-func makeHandler(fn func (*http.Conn, *http.Request, string)) http.HandlerFunc {
-	return func(c *http.Conn, r *http.Request) {
+func makeHandler(fn func (http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
 		// Here we will extract the page title from the Request,
 		// and call the provided handler 'fn'
 	}
@@ -881,28 +878,28 @@ Now we can take the code from getTitle and use it here
 

-func makeHandler(fn func(*http.Conn, *http.Request, string)) http.HandlerFunc {
-	return func(c *http.Conn, r *http.Request) {
+func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
 		title := r.URL.Path[lenPath:]
 		if !titleValidator.MatchString(title) {
-			http.NotFound(c, r)
+			http.NotFound(w, r)
 			return
 		}
-		fn(c, r, title)
+		fn(w, r, title)
 	}
 }
 

The closure returned by makeHandler is a function that takes -an http.Conn and http.Request (in other words, -an http.HandlerFunc). +an http.ResponseWriter and http.Request (in other +words, an http.HandlerFunc). The closure extracts the title from the request path, and validates it with the titleValidator regexp. If the title is invalid, an error will be written to the -Conn using the http.NotFound function. +ResponseWriter using the http.NotFound function. If the title is valid, the enclosed handler function -fn will be called with the Conn, +fn will be called with the ResponseWriter, Request, and title as arguments.

@@ -927,32 +924,32 @@ making them much simpler:

-func viewHandler(c *http.Conn, r *http.Request, title string) {
+func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
 	p, err := loadPage(title)
 	if err != nil {
-		http.Redirect(c, "/edit/"+title, http.StatusFound)
+		http.Redirect(w, r, "/edit/"+title, http.StatusFound)
 		return
 	}
-	renderTemplate(c, "view", p)
+	renderTemplate(w, "view", p)
 }
 
-func editHandler(c *http.Conn, r *http.Request, title string) {
+func editHandler(w http.ResponseWriter, r *http.Request, title string) {
 	p, err := loadPage(title)
 	if err != nil {
 		p = &page{title: title}
 	}
-	renderTemplate(c, "edit", p)
+	renderTemplate(w, "edit", p)
 }
 
-func saveHandler(c *http.Conn, r *http.Request, title string) {
+func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
 	body := r.FormValue("body")
 	p := &page{title: title, body: []byte(body)}
 	err := p.save()
 	if err != nil {
-		http.Error(c, err.String(), http.StatusInternalServerError)
+		http.Error(w, err.String(), http.StatusInternalServerError)
 		return
 	}
-	http.Redirect(c, "/view/"+title, http.StatusFound)
+	http.Redirect(w, r, "/view/"+title, http.StatusFound)
 }
 
@@ -996,5 +993,3 @@ Here are some simple tasks you might want to tackle on your own: (hint: you could use regexp.ReplaceAllFunc to do this) - -
diff --git a/doc/codelab/wiki/notemplate.go b/doc/codelab/wiki/notemplate.go index a61d905e3..c1f952c83 100644 --- a/doc/codelab/wiki/notemplate.go +++ b/doc/codelab/wiki/notemplate.go @@ -28,19 +28,19 @@ func loadPage(title string) (*page, os.Error) { const lenPath = len("/view/") -func viewHandler(c *http.Conn, r *http.Request) { +func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) - fmt.Fprintf(c, "

%s

%s
", p.title, p.body) + fmt.Fprintf(w, "

%s

%s
", p.title, p.body) } -func editHandler(c *http.Conn, r *http.Request) { +func editHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, err := loadPage(title) if err != nil { p = &page{title: title} } - fmt.Fprintf(c, "

Editing %s

"+ + fmt.Fprintf(w, "

Editing %s

"+ "
"+ "
"+ ""+ diff --git a/doc/codelab/wiki/part2.go b/doc/codelab/wiki/part2.go index c2c29dc3b..8d4454a74 100644 --- a/doc/codelab/wiki/part2.go +++ b/doc/codelab/wiki/part2.go @@ -28,10 +28,10 @@ func loadPage(title string) (*page, os.Error) { const lenPath = len("/view/") -func viewHandler(c *http.Conn, r *http.Request) { +func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) - fmt.Fprintf(c, "

%s

%s
", p.title, p.body) + fmt.Fprintf(w, "

%s

%s
", p.title, p.body) } func main() { diff --git a/doc/codelab/wiki/srcextract.go b/doc/codelab/wiki/srcextract.go index 607375183..0addc61c4 100644 --- a/doc/codelab/wiki/srcextract.go +++ b/doc/codelab/wiki/srcextract.go @@ -25,7 +25,7 @@ func main() { os.Exit(2) } // load file - file, err := parser.ParseFile(*srcFn, nil, nil, 0) + file, err := parser.ParseFile(*srcFn, nil, 0) if err != nil { log.Exit(err) } diff --git a/doc/codelab/wiki/wiki.html b/doc/codelab/wiki/wiki.html index c7f44ded4..919385edf 100644 --- a/doc/codelab/wiki/wiki.html +++ b/doc/codelab/wiki/wiki.html @@ -1,7 +1,4 @@ -
- -

Writing Web Applications

- +

Introduction

@@ -233,12 +230,12 @@ This function will block until the program is terminated.

The function handler is of the type http.HandlerFunc. -It takes an http.Conn and http.Request as its -arguments. +It takes an http.ResponseWriter and an http.Request as +its arguments.

-An http.Conn is the server end of an HTTP connection; by writing +An http.ResponseWriter value assembles the HTTP server's response; by writing to it, we send data to the HTTP client.

@@ -296,7 +293,7 @@ begin with "/view/", which is not part of the page title.

The function then loads the page data, formats the page with a string of simple -HTML, and writes it to c, the http.Conn. +HTML, and writes it to w, the http.ResponseWriter.

@@ -418,7 +415,7 @@ The function template.ParseFile will read the contents of The method t.Execute replaces all occurrences of {title} and {body} with the values of p.title and p.body, and writes the resultant -HTML to the http.Conn. +HTML to the http.ResponseWriter.

@@ -670,9 +667,9 @@ a title string:

-func viewHandler(c *http.Conn, r *http.Request, title string)
-func editHandler(c *http.Conn, r *http.Request, title string)
-func saveHandler(c *http.Conn, r *http.Request, title string)
+func viewHandler(w http.ResponseWriter, r *http.Request, title string)
+func editHandler(w http.ResponseWriter, r *http.Request, title string)
+func saveHandler(w http.ResponseWriter, r *http.Request, title string)
 

@@ -682,8 +679,8 @@ type, and returns a function of type http.HandlerFunc

-func makeHandler(fn func (*http.Conn, *http.Request, string)) http.HandlerFunc {
-	return func(c *http.Conn, r *http.Request) {
+func makeHandler(fn func (http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
 		// Here we will extract the page title from the Request,
 		// and call the provided handler 'fn'
 	}
@@ -708,14 +705,14 @@ Now we can take the code from getTitle and use it here
 
 

The closure returned by makeHandler is a function that takes -an http.Conn and http.Request (in other words, -an http.HandlerFunc). +an http.ResponseWriter and http.Request (in other +words, an http.HandlerFunc). The closure extracts the title from the request path, and validates it with the titleValidator regexp. If the title is invalid, an error will be written to the -Conn using the http.NotFound function. +ResponseWriter using the http.NotFound function. If the title is valid, the enclosed handler function -fn will be called with the Conn, +fn will be called with the ResponseWriter, Request, and title as arguments.

@@ -782,5 +779,3 @@ Here are some simple tasks you might want to tackle on your own: (hint: you could use regexp.ReplaceAllFunc to do this) - -
-- cgit v1.2.3