From: Shenghou Ma Date: Fri, 24 Feb 2012 17:09:05 +0000 (+0800) Subject: doc: update codelab/wiki to Go 1. X-Git-Tag: weekly.2012-03-04~164 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=52cd4c8610561bfe0ba06ecb6dbb540128ffc7e8;p=gostls13.git doc: update codelab/wiki to Go 1. R=golang-dev, r, adg CC=golang-dev https://golang.org/cl/5683076 --- diff --git a/doc/codelab/wiki/Makefile b/doc/codelab/wiki/Makefile index 233917f2c6..0cb9071850 100644 --- a/doc/codelab/wiki/Makefile +++ b/doc/codelab/wiki/Makefile @@ -2,13 +2,9 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../src/Make.inc - all: index.html -include ../../../src/Make.common - -CLEANFILES+=srcextract.bin htmlify.bin get.bin +CLEANFILES:=srcextract.bin htmlify.bin get.bin index.html: wiki.html srcextract.bin htmlify.bin PATH=.:$$PATH awk '/^!/{system(substr($$0,2)); next} {print}' < wiki.html | tr -d '\r' > index.html @@ -17,9 +13,8 @@ test: get.bin bash ./test.sh rm -f get.6 get.bin -%.bin: %.$O - $(LD) -o $@ $< - -%.$O: %.go - $(GC) $(GCFLAGS) $(GCIMPORTS) $*.go +%.bin: %.go + go build -o $@ $^ +clean: + rm -f $(CLEANFILES) diff --git a/doc/codelab/wiki/final-noclosure.go b/doc/codelab/wiki/final-noclosure.go index a0428d42dc..a23cf7a27a 100644 --- a/doc/codelab/wiki/final-noclosure.go +++ b/doc/codelab/wiki/final-noclosure.go @@ -6,10 +6,10 @@ package main import ( "errors" + "html/template" "io/ioutil" "net/http" "regexp" - "text/template" ) type Page struct { diff --git a/doc/codelab/wiki/final-noerror.go b/doc/codelab/wiki/final-noerror.go index e86bc1a3ca..e11d268e2f 100644 --- a/doc/codelab/wiki/final-noerror.go +++ b/doc/codelab/wiki/final-noerror.go @@ -5,9 +5,9 @@ package main import ( + "html/template" "io/ioutil" "net/http" - "text/template" ) type Page struct { diff --git a/doc/codelab/wiki/final-parsetemplate.go b/doc/codelab/wiki/final-parsetemplate.go index c068a616ff..6234c08f2e 100644 --- a/doc/codelab/wiki/final-parsetemplate.go +++ b/doc/codelab/wiki/final-parsetemplate.go @@ -5,10 +5,10 @@ package main import ( + "html/template" "io/ioutil" "net/http" "regexp" - "text/template" ) type Page struct { @@ -59,7 +59,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request, title string) { } func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { - t, err := template.ParseFiles(tmpl+".html", nil) + t, err := template.ParseFiles(tmpl + ".html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/doc/codelab/wiki/final-template.go b/doc/codelab/wiki/final-template.go index 5386210a5c..f295b9d600 100644 --- a/doc/codelab/wiki/final-template.go +++ b/doc/codelab/wiki/final-template.go @@ -5,9 +5,9 @@ package main import ( + "html/template" "io/ioutil" "net/http" - "text/template" ) type Page struct { @@ -55,7 +55,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request) { } func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { - t, _ := template.ParseFiles(tmpl+".html", nil) + t, _ := template.ParseFiles(tmpl + ".html") t.Execute(w, p) } diff --git a/doc/codelab/wiki/final.go b/doc/codelab/wiki/final.go index 97f0a16a66..134ad7e63c 100644 --- a/doc/codelab/wiki/final.go +++ b/doc/codelab/wiki/final.go @@ -5,10 +5,10 @@ package main import ( + "html/template" "io/ioutil" "net/http" "regexp" - "text/template" ) type Page struct { diff --git a/doc/codelab/wiki/index.html b/doc/codelab/wiki/index.html index ae71a402ef..efb647298b 100644 --- a/doc/codelab/wiki/index.html +++ b/doc/codelab/wiki/index.html @@ -6,8 +6,8 @@ Covered in this codelab:

@@ -18,21 +18,18 @@ Assumed knowledge:

Getting Started

-At present, you need to have a Linux, OS X, or FreeBSD machine to run Go. If -you don't have access to one, you could set up a Linux Virtual Machine (using -VirtualBox or similar) or a -Virtual -Private Server. +At present, you need to have a FreeBSD, Linux, OS X, or Windows machine to run Go. +We will use $ to represent the command prompt.

-Install Go (see the Installation Instructions). +Install Go (see the Installation Instructions).

@@ -40,8 +37,8 @@ Make a new directory for this codelab and cd to it:

-$ mkdir ~/gowiki
-$ cd ~/gowiki
+$ mkdir gowiki
+$ cd gowiki
 

@@ -55,15 +52,13 @@ package main import ( "fmt" "io/ioutil" - "os" )

-We import the fmt, ioutil and os -packages from the Go standard library. Later, as we implement additional -functionality, we will add more packages to this import -declaration. +We import the fmt and ioutil packages from the Go +standard library. Later, as we implement additional functionality, we will +add more packages to this import declaration.

Data Structures

@@ -84,8 +79,8 @@ type Page struct {

The type []byte means "a byte slice". -(See Effective Go -for more on slices.) +(See Slices: usage and +internals for more on slices.) The Body element is a []byte rather than string because that is the type expected by the io libraries we will use, as you'll see below. @@ -178,9 +173,8 @@ func loadPage(title string) (*Page, error) {

Callers of this function can now check the second parameter; if it is nil then it has successfully loaded a Page. If not, it will be an -error that can be handled by the caller (see the os package documentation for -details). +error that can be handled by the caller (see the +language specification for details).

@@ -210,23 +204,21 @@ You can compile and run the program like this:

-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 This is a sample page.
 

-(The 8g and 8l commands are applicable to -GOARCH=386. If you're on an amd64 system, -substitute 6's for the 8's.) +(If you're using Windows you must type "wiki" without the +"./" to run the program.)

Click here to view the code we've written so far.

-

Introducing the http package (an interlude)

+

Introducing the net/http package (an interlude)

Here's a full working example of a simple web server: @@ -292,18 +284,17 @@ the program would present a page containing:

Hi there, I love monkeys!
-

Using http to serve wiki pages

+

Using net/http to serve wiki pages

-To use the http package, it must be imported: +To use the net/http package, it must be imported:

 import (
 	"fmt"
-	"http"
+	"net/http"
 	"io/ioutil"
-	"os"
 )
 
@@ -361,14 +352,17 @@ func main() {

Let's create some page data (as test.txt), compile our code, and -try serving a wiki page: +try serving a wiki page. +

+ +

+Open test.txt file in your editor, and save the string "Hello world" (without quotes) +in it.

-$ echo "Hello world" > test.txt
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 

@@ -426,19 +420,17 @@ This function will work fine, but all that hard-coded HTML is ugly. Of course, there is a better way.

-

The template package

+

The html/template package

-The template package is part of the Go standard library. -(A new template package is coming; this code lab will be updated soon.) -We can -use template to keep the HTML in a separate file, allowing -us to change the layout of our edit page without modifying the underlying Go -code. +The html/template package is part of the Go standard library. +We can use html/template to keep the HTML in a separate file, +allowing us to change the layout of our edit page without modifying the +underlying Go code.

-First, we must add template to the list of imports: +First, we must add html/template to the list of imports:

@@ -446,7 +438,7 @@ import (
 	"http"
 	"io/ioutil"
 	"os"
-	"template"
+	"html/template"
 )
 
@@ -482,7 +474,7 @@ func editHandler(w http.ResponseWriter, r *http.Request) {

-The function template.ParseFile will read the contents of +The function template.ParseFiles will read the contents of edit.html and return a *template.Template.

@@ -558,7 +550,7 @@ func editHandler(w http.ResponseWriter, r *http.Request) { } func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { - t, _ := template.ParseFiles(tmpl+".html", nil) + t, _ := template.ParseFiles(tmpl + ".html") t.Execute(w, p) } @@ -570,10 +562,11 @@ The handlers are now shorter and simpler.

Handling non-existent pages

-What if you visit /view/APageThatDoesntExist? The program will -crash. This is because it ignores the error return value from -loadPage. Instead, if the requested Page doesn't exist, it should -redirect the client to the edit Page so the content may be created: +What if you visit +/view/APageThatDoesntExist? The program will crash. This is +because it ignores the error return value from loadPage. Instead, +if the requested Page doesn't exist, it should redirect the client to the edit +Page so the content may be created:

@@ -643,7 +636,7 @@ First, let's handle the errors in renderTemplate:
 
 
 func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
-	t, err := template.ParseFiles(tmpl+".html", nil)
+	t, err := template.ParseFiles(tmpl + ".html")
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		return
@@ -976,9 +969,8 @@ Recompile the code, and run the app:
 

-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 

diff --git a/doc/codelab/wiki/test.sh b/doc/codelab/wiki/test.sh index ed63ff20ff..58b218a78a 100755 --- a/doc/codelab/wiki/test.sh +++ b/doc/codelab/wiki/test.sh @@ -8,10 +8,10 @@ cleanup() { } trap cleanup 0 INT -gomake get.bin +make get.bin addr=$(./get.bin -addr) sed s/:8080/$addr/ < final.go > final-test.go -gomake final-test.bin +make final-test.bin (./final-test.bin) & wiki_pid=$! diff --git a/doc/codelab/wiki/wiki.html b/doc/codelab/wiki/wiki.html index c3dee3f709..8a22f3db2b 100644 --- a/doc/codelab/wiki/wiki.html +++ b/doc/codelab/wiki/wiki.html @@ -6,8 +6,8 @@ Covered in this codelab:

  • Creating a data structure with load and save methods
  • -
  • Using the http package to build web applications -
  • Using the template package to process HTML templates
  • +
  • Using the net/http package to build web applications +
  • Using the html/template package to process HTML templates
  • Using the regexp package to validate user input
  • Using closures
@@ -18,21 +18,18 @@ Assumed knowledge:
  • Programming experience
  • Understanding of basic web technologies (HTTP, HTML)
  • -
  • Some UNIX command-line knowledge
  • +
  • Some UNIX/DOS command-line knowledge

Getting Started

-At present, you need to have a Linux, OS X, or FreeBSD machine to run Go. If -you don't have access to one, you could set up a Linux Virtual Machine (using -VirtualBox or similar) or a -Virtual -Private Server. +At present, you need to have a FreeBSD, Linux, OS X, or Windows machine to run Go. +We will use $ to represent the command prompt.

-Install Go (see the Installation Instructions). +Install Go (see the Installation Instructions).

@@ -40,8 +37,8 @@ Make a new directory for this codelab and cd to it:

-$ mkdir ~/gowiki
-$ cd ~/gowiki
+$ mkdir gowiki
+$ cd gowiki
 

@@ -55,15 +52,13 @@ package main import ( "fmt" "io/ioutil" - "os" )

-We import the fmt, ioutil and os -packages from the Go standard library. Later, as we implement additional -functionality, we will add more packages to this import -declaration. +We import the fmt and ioutil packages from the Go +standard library. Later, as we implement additional functionality, we will +add more packages to this import declaration.

Data Structures

@@ -81,8 +76,8 @@ the title and body.

The type []byte means "a byte slice". -(See Effective Go -for more on slices.) +(See Slices: usage and +internals for more on slices.) The Body element is a []byte rather than string because that is the type expected by the io libraries we will use, as you'll see below. @@ -161,9 +156,8 @@ function to return *Page and error.

Callers of this function can now check the second parameter; if it is nil then it has successfully loaded a Page. If not, it will be an -error that can be handled by the caller (see the os package documentation for -details). +error that can be handled by the caller (see the +language specification for details).

@@ -188,23 +182,21 @@ You can compile and run the program like this:

-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 This is a sample page.
 

-(The 8g and 8l commands are applicable to -GOARCH=386. If you're on an amd64 system, -substitute 6's for the 8's.) +(If you're using Windows you must type "wiki" without the +"./" to run the program.)

Click here to view the code we've written so far.

-

Introducing the http package (an interlude)

+

Introducing the net/http package (an interlude)

Here's a full working example of a simple web server: @@ -256,18 +248,17 @@ the program would present a page containing:

Hi there, I love monkeys!
-

Using http to serve wiki pages

+

Using net/http to serve wiki pages

-To use the http package, it must be imported: +To use the net/http package, it must be imported:

 import (
 	"fmt"
-	"http"
+	"net/http"
 	"io/ioutil"
-	"os"
 )
 
@@ -318,14 +309,17 @@ any requests under the path /view/.

Let's create some page data (as test.txt), compile our code, and -try serving a wiki page: +try serving a wiki page. +

+ +

+Open test.txt file in your editor, and save the string "Hello world" (without quotes) +in it.

-$ echo "Hello world" > test.txt
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 

@@ -366,19 +360,17 @@ This function will work fine, but all that hard-coded HTML is ugly. Of course, there is a better way.

-

The template package

+

The html/template package

-The template package is part of the Go standard library. -(A new template package is coming; this code lab will be updated soon.) -We can -use template to keep the HTML in a separate file, allowing -us to change the layout of our edit page without modifying the underlying Go -code. +The html/template package is part of the Go standard library. +We can use html/template to keep the HTML in a separate file, +allowing us to change the layout of our edit page without modifying the +underlying Go code.

-First, we must add template to the list of imports: +First, we must add html/template to the list of imports:

@@ -386,7 +378,7 @@ import (
 	"http"
 	"io/ioutil"
 	"os"
-	"template"
+	"html/template"
 )
 
@@ -409,7 +401,7 @@ HTML:

-The function template.ParseFile will read the contents of +The function template.ParseFiles will read the contents of edit.html and return a *template.Template.

@@ -474,10 +466,11 @@ The handlers are now shorter and simpler.

Handling non-existent pages

-What if you visit /view/APageThatDoesntExist? The program will -crash. This is because it ignores the error return value from -loadPage. Instead, if the requested Page doesn't exist, it should -redirect the client to the edit Page so the content may be created: +What if you visit +/view/APageThatDoesntExist? The program will crash. This is +because it ignores the error return value from loadPage. Instead, +if the requested Page doesn't exist, it should redirect the client to the edit +Page so the content may be created:

@@ -753,9 +746,8 @@ Recompile the code, and run the app:
 

-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki