mirror of
https://github.com/golang/go
synced 2024-11-24 22:57:57 -07:00
Codelab: correct function definitions for handlers before closures are introduced.
A couple of post-closure function definitions were introduced too early, making the resulting code fail compilation. Also, the TitleValidator regexp was missing. R=adg CC=golang-dev https://golang.org/cl/4105054
This commit is contained in:
parent
ab2aca5e52
commit
61c93cac3e
@ -573,7 +573,11 @@ redirect the client to the edit Page so the content may be created:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
title, err := getTitle(w, r)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
p, err := loadPage(title)
|
p, err := loadPage(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
||||||
@ -658,10 +662,14 @@ Now let's fix up <code>saveHandler</code>:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
title, err := getTitle(w, r)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
body := r.FormValue("body")
|
body := r.FormValue("body")
|
||||||
p := &Page{Title: title, Body: []byte(body)}
|
p := &Page{Title: title, Body: []byte(body)}
|
||||||
err := p.save()
|
err = p.save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.String(), http.StatusInternalServerError)
|
http.Error(w, err.String(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -747,6 +755,7 @@ Then we can create a global variable to store our validation regexp:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
|
var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
@ -477,7 +477,7 @@ redirect the client to the edit Page so the content may be created:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
!./srcextract.bin -src=final.go -name=viewHandler
|
!./srcextract.bin -src=final-noclosure.go -name=viewHandler
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -539,7 +539,7 @@ Now let's fix up <code>saveHandler</code>:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
!./srcextract.bin -src=final.go -name=saveHandler
|
!./srcextract.bin -src=final-noclosure.go -name=saveHandler
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -610,7 +610,7 @@ Then we can create a global variable to store our validation regexp:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
!./srcextract.bin -src=final-noclosure.go -name=TitleValidator
|
!./srcextract.bin -src=final-noclosure.go -name=titleValidator
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
Loading…
Reference in New Issue
Block a user