]> Cypherpunks repositories - gostls13.git/commitdiff
doc: update codelab/wiki to Go 1.
authorShenghou Ma <minux.ma@gmail.com>
Fri, 24 Feb 2012 17:09:05 +0000 (01:09 +0800)
committerShenghou Ma <minux.ma@gmail.com>
Fri, 24 Feb 2012 17:09:05 +0000 (01:09 +0800)
R=golang-dev, r, adg
CC=golang-dev
https://golang.org/cl/5683076

doc/codelab/wiki/Makefile
doc/codelab/wiki/final-noclosure.go
doc/codelab/wiki/final-noerror.go
doc/codelab/wiki/final-parsetemplate.go
doc/codelab/wiki/final-template.go
doc/codelab/wiki/final.go
doc/codelab/wiki/index.html
doc/codelab/wiki/test.sh
doc/codelab/wiki/wiki.html

index 233917f2c61d419fab20996c29fc481486658f9a..0cb9071850e48a15ddba2551567e7063c3272c8c 100644 (file)
@@ -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)
index a0428d42dc955b87f79e4e4421ac239f0b310dc9..a23cf7a27a858b05ad250fc6a0ba8d9b8006acb2 100644 (file)
@@ -6,10 +6,10 @@ package main
 
 import (
        "errors"
+       "html/template"
        "io/ioutil"
        "net/http"
        "regexp"
-       "text/template"
 )
 
 type Page struct {
index e86bc1a3ca995f5ebb84ddbdfc151fa64974aab2..e11d268e2faab0ae527c700b014660d81e08ff13 100644 (file)
@@ -5,9 +5,9 @@
 package main
 
 import (
+       "html/template"
        "io/ioutil"
        "net/http"
-       "text/template"
 )
 
 type Page struct {
index c068a616ff47e8454bc7e7a5f3ed93094e968a50..6234c08f2e3ed0d24776d0090a47c0549bd213f3 100644 (file)
@@ -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
index 5386210a5caf7581608b70dd610a8cdcba8f565e..f295b9d600087e8b5785e56aa0068e9283de26bc 100644 (file)
@@ -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)
 }
 
index 97f0a16a663e27348d70c7716226a1aa8459deaa..134ad7e63cb7f240b260e304a780b152942a9ebc 100644 (file)
@@ -5,10 +5,10 @@
 package main
 
 import (
+       "html/template"
        "io/ioutil"
        "net/http"
        "regexp"
-       "text/template"
 )
 
 type Page struct {
index ae71a402ef81fbc7115dfdecaa403ee526fd2b92..efb647298bd7ab784c30d006cf709dd39717f732 100644 (file)
@@ -6,8 +6,8 @@ Covered in this codelab:
 </p>
 <ul>
 <li>Creating a data structure with load and save methods</li>
-<li>Using the <code>http</code> package to build web applications
-<li>Using the <code>template</code> package to process HTML templates</li>
+<li>Using the <code>net/http</code> package to build web applications
+<li>Using the <code>html/template</code> package to process HTML templates</li>
 <li>Using the <code>regexp</code> package to validate user input</li>
 <li>Using closures</li>
 </ul>
@@ -18,21 +18,18 @@ Assumed knowledge:
 <ul>
 <li>Programming experience</li>
 <li>Understanding of basic web technologies (HTTP, HTML)</li>
-<li>Some UNIX command-line knowledge</li>
+<li>Some UNIX/DOS command-line knowledge</li>
 </ul>
 
 <h2>Getting Started</h2>
 
 <p>
-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 
-<a href="http://www.virtualbox.org/">VirtualBox</a> or similar) or a
-<a href="http://www.google.com/search?q=virtual+private+server">Virtual 
-Private Server</a>.
+At present, you need to have a FreeBSD, Linux, OS X, or Windows machine to run Go.
+We will use <code>$</code> to represent the command prompt.
 </p>
 
 <p>
-Install Go (see the <a href="http://golang.org/doc/install.html">Installation Instructions</a>).
+Install Go (see the <a href="/doc/install.html">Installation Instructions</a>).
 </p>
 
 <p>
@@ -40,8 +37,8 @@ Make a new directory for this codelab and cd to it:
 </p>
 
 <pre>
-$ mkdir ~/gowiki
-$ cd ~/gowiki
+$ mkdir gowiki
+$ cd gowiki
 </pre>
 
 <p>
@@ -55,15 +52,13 @@ package main
 import (
        "fmt"
        "io/ioutil"
-       "os"
 )
 </pre>
 
 <p>
-We import the <code>fmt</code>, <code>ioutil</code> and <code>os</code>
-packages from the Go standard library. Later, as we implement additional
-functionality, we will add more packages to this <code>import</code>
-declaration.
+We import the <code>fmt</code> and <code>ioutil</code> packages from the Go 
+standard library. Later, as we implement additional functionality, we will 
+add more packages to this <code>import</code> declaration.
 </p>
 
 <h2>Data Structures</h2>
@@ -84,8 +79,8 @@ type Page struct {
 
 <p>
 The type <code>[]byte</code> means "a <code>byte</code> slice". 
-(See <a href="http://golang.org/doc/effective_go.html#slices">Effective Go</a> 
-for more on slices.)  
+(See <a href="/doc/articles/slices_usage_and_internals.html">Slices: usage and
+internals</a> for more on slices.)
 The <code>Body</code> element is a <code>[]byte</code> rather than
 <code>string</code> because that is the type expected by the <code>io</code>
 libraries we will use, as you'll see below.
@@ -178,9 +173,8 @@ func loadPage(title string) (*Page, error) {
 <p>
 Callers of this function can now check the second parameter; if it is
 <code>nil</code> then it has successfully loaded a Page. If not, it will be an
-<code>error</code> that can be handled by the caller (see the <a
-href="http://golang.org/pkg/os/#Error">os package documentation</a> for 
-details).
+<code>error</code> that can be handled by the caller (see the 
+<a href="/doc/go_spec.html#Errors">language specification</a> for details).
 </p>
 
 <p>
@@ -210,23 +204,21 @@ You can compile and run the program like this:
 </p>
 
 <pre>
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 This is a sample page.
 </pre>
 
 <p>
-(The <code>8g</code> and <code>8l</code> commands are applicable to
-<code>GOARCH=386</code>. If you're on an <code>amd64</code> system,
-substitute 6's for the 8's.)
+(If you're using Windows you must type "<code>wiki</code>" without the 
+"<code>./</code>" to run the program.)
 </p>
 
 <p>
 <a href="part1.go">Click here to view the code we've written so far.</a>
 </p>
 
-<h2>Introducing the <code>http</code> package (an interlude)</h2>
+<h2>Introducing the <code>net/http</code> package (an interlude)</h2>
 
 <p>
 Here's a full working example of a simple web server:
@@ -292,18 +284,17 @@ the program would present a page containing:
 </p>
 <pre>Hi there, I love monkeys!</pre>
 
-<h2>Using <code>http</code> to serve wiki pages</h2>
+<h2>Using <code>net/http</code> to serve wiki pages</h2>
 
 <p>
-To use the <code>http</code> package, it must be imported:
+To use the <code>net/http</code> package, it must be imported:
 </p>
 
 <pre>
 import (
        "fmt"
-       <b>"http"</b>
+       <b>"net/http"</b>
        "io/ioutil"
-       "os"
 )
 </pre>
 
@@ -361,14 +352,17 @@ func main() {
 
 <p>
 Let's create some page data (as <code>test.txt</code>), compile our code, and
-try serving a wiki page:
+try serving a wiki page.
+</p>
+
+<p>
+Open <code>test.txt</code> file in your editor, and save the string "Hello world" (without quotes)
+in it.
 </p>
 
 <pre>
-$ echo "Hello world" &gt; test.txt
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 </pre>
 
 <p>
@@ -426,19 +420,17 @@ This function will work fine, but all that hard-coded HTML is ugly.
 Of course, there is a better way.
 </p>
  
-<h2>The <code>template</code> package</h2>
+<h2>The <code>html/template</code> package</h2>
 
 <p>
-The <code>template</code> package is part of the Go standard library.
-(A new template package is coming; this code lab will be updated soon.)
-We can
-use <code>template</code> 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 <code>html/template</code> package is part of the Go standard library.
+We can use <code>html/template</code> to keep the HTML in a separate file,
+allowing us to change the layout of our edit page without modifying the
+underlying Go code.
 </p>
 
 <p>
-First, we must add <code>template</code> to the list of imports:
+First, we must add <code>html/template</code> to the list of imports:
 </p>
 
 <pre>
@@ -446,7 +438,7 @@ import (
        "http"
        "io/ioutil"
        "os"
-       <b>"template"</b>
+       <b>"html/template"</b>
 )
 </pre>
 
@@ -482,7 +474,7 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
 </pre>
 
 <p>
-The function <code>template.ParseFile</code> will read the contents of 
+The function <code>template.ParseFiles</code> will read the contents of 
 <code>edit.html</code> and return a <code>*template.Template</code>. 
 </p>
 
@@ -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+&#34;.html&#34;, nil)
+       t, _ := template.ParseFiles(tmpl + &#34;.html&#34;)
        t.Execute(w, p)
 }
 </pre>
@@ -570,10 +562,11 @@ The handlers are now shorter and simpler.
 <h2>Handling non-existent pages</h2>
 
 <p>
-What if you visit <code>/view/APageThatDoesntExist</code>? The program will 
-crash. This is because it ignores the error return value from
-<code>loadPage</code>. 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 <a href="http://localhost:8080/view/APageThatDoesntExist">
+<code>/view/APageThatDoesntExist</code></a>? The program will crash. This is 
+because it ignores the error return value from <code>loadPage</code>. Instead,
+if the requested Page doesn't exist, it should redirect the client to the edit
+Page so the content may be created:
 </p>
 
 <pre>
@@ -643,7 +636,7 @@ First, let's handle the errors in <code>renderTemplate</code>:
 
 <pre>
 func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
-       t, err := template.ParseFiles(tmpl+&#34;.html&#34;, nil)
+       t, err := template.ParseFiles(tmpl + &#34;.html&#34;)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
@@ -976,9 +969,8 @@ Recompile the code, and run the app:
 </p>
 
 <pre>
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 </pre>
 
 <p>
index ed63ff20ffd7425f495726615f9f368dc0fd1ac5..58b218a78a556d4952b62b8da9907bd62d31874e 100755 (executable)
@@ -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=$!
 
index c3dee3f709f9d1625d4b4096ceea473c55a7d42c..8a22f3db2b798662e03cc0f8849694dbd345e571 100644 (file)
@@ -6,8 +6,8 @@ Covered in this codelab:
 </p>
 <ul>
 <li>Creating a data structure with load and save methods</li>
-<li>Using the <code>http</code> package to build web applications
-<li>Using the <code>template</code> package to process HTML templates</li>
+<li>Using the <code>net/http</code> package to build web applications
+<li>Using the <code>html/template</code> package to process HTML templates</li>
 <li>Using the <code>regexp</code> package to validate user input</li>
 <li>Using closures</li>
 </ul>
@@ -18,21 +18,18 @@ Assumed knowledge:
 <ul>
 <li>Programming experience</li>
 <li>Understanding of basic web technologies (HTTP, HTML)</li>
-<li>Some UNIX command-line knowledge</li>
+<li>Some UNIX/DOS command-line knowledge</li>
 </ul>
 
 <h2>Getting Started</h2>
 
 <p>
-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 
-<a href="http://www.virtualbox.org/">VirtualBox</a> or similar) or a
-<a href="http://www.google.com/search?q=virtual+private+server">Virtual 
-Private Server</a>.
+At present, you need to have a FreeBSD, Linux, OS X, or Windows machine to run Go.
+We will use <code>$</code> to represent the command prompt.
 </p>
 
 <p>
-Install Go (see the <a href="http://golang.org/doc/install.html">Installation Instructions</a>).
+Install Go (see the <a href="/doc/install.html">Installation Instructions</a>).
 </p>
 
 <p>
@@ -40,8 +37,8 @@ Make a new directory for this codelab and cd to it:
 </p>
 
 <pre>
-$ mkdir ~/gowiki
-$ cd ~/gowiki
+$ mkdir gowiki
+$ cd gowiki
 </pre>
 
 <p>
@@ -55,15 +52,13 @@ package main
 import (
        "fmt"
        "io/ioutil"
-       "os"
 )
 </pre>
 
 <p>
-We import the <code>fmt</code>, <code>ioutil</code> and <code>os</code>
-packages from the Go standard library. Later, as we implement additional
-functionality, we will add more packages to this <code>import</code>
-declaration.
+We import the <code>fmt</code> and <code>ioutil</code> packages from the Go 
+standard library. Later, as we implement additional functionality, we will 
+add more packages to this <code>import</code> declaration.
 </p>
 
 <h2>Data Structures</h2>
@@ -81,8 +76,8 @@ the title and body.
 
 <p>
 The type <code>[]byte</code> means "a <code>byte</code> slice". 
-(See <a href="http://golang.org/doc/effective_go.html#slices">Effective Go</a> 
-for more on slices.)  
+(See <a href="/doc/articles/slices_usage_and_internals.html">Slices: usage and
+internals</a> for more on slices.)
 The <code>Body</code> element is a <code>[]byte</code> rather than
 <code>string</code> because that is the type expected by the <code>io</code>
 libraries we will use, as you'll see below.
@@ -161,9 +156,8 @@ function to return <code>*Page</code> and <code>error</code>.
 <p>
 Callers of this function can now check the second parameter; if it is
 <code>nil</code> then it has successfully loaded a Page. If not, it will be an
-<code>error</code> that can be handled by the caller (see the <a
-href="http://golang.org/pkg/os/#Error">os package documentation</a> for 
-details).
+<code>error</code> that can be handled by the caller (see the 
+<a href="/doc/go_spec.html#Errors">language specification</a> for details).
 </p>
 
 <p>
@@ -188,23 +182,21 @@ You can compile and run the program like this:
 </p>
 
 <pre>
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 This is a sample page.
 </pre>
 
 <p>
-(The <code>8g</code> and <code>8l</code> commands are applicable to
-<code>GOARCH=386</code>. If you're on an <code>amd64</code> system,
-substitute 6's for the 8's.)
+(If you're using Windows you must type "<code>wiki</code>" without the 
+"<code>./</code>" to run the program.)
 </p>
 
 <p>
 <a href="part1.go">Click here to view the code we've written so far.</a>
 </p>
 
-<h2>Introducing the <code>http</code> package (an interlude)</h2>
+<h2>Introducing the <code>net/http</code> package (an interlude)</h2>
 
 <p>
 Here's a full working example of a simple web server:
@@ -256,18 +248,17 @@ the program would present a page containing:
 </p>
 <pre>Hi there, I love monkeys!</pre>
 
-<h2>Using <code>http</code> to serve wiki pages</h2>
+<h2>Using <code>net/http</code> to serve wiki pages</h2>
 
 <p>
-To use the <code>http</code> package, it must be imported:
+To use the <code>net/http</code> package, it must be imported:
 </p>
 
 <pre>
 import (
        "fmt"
-       <b>"http"</b>
+       <b>"net/http"</b>
        "io/ioutil"
-       "os"
 )
 </pre>
 
@@ -318,14 +309,17 @@ any requests under the path <code>/view/</code>.
 
 <p>
 Let's create some page data (as <code>test.txt</code>), compile our code, and
-try serving a wiki page:
+try serving a wiki page.
+</p>
+
+<p>
+Open <code>test.txt</code> file in your editor, and save the string "Hello world" (without quotes)
+in it.
 </p>
 
 <pre>
-$ echo "Hello world" &gt; test.txt
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 </pre>
 
 <p>
@@ -366,19 +360,17 @@ This function will work fine, but all that hard-coded HTML is ugly.
 Of course, there is a better way.
 </p>
  
-<h2>The <code>template</code> package</h2>
+<h2>The <code>html/template</code> package</h2>
 
 <p>
-The <code>template</code> package is part of the Go standard library.
-(A new template package is coming; this code lab will be updated soon.)
-We can
-use <code>template</code> 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 <code>html/template</code> package is part of the Go standard library.
+We can use <code>html/template</code> to keep the HTML in a separate file,
+allowing us to change the layout of our edit page without modifying the
+underlying Go code.
 </p>
 
 <p>
-First, we must add <code>template</code> to the list of imports:
+First, we must add <code>html/template</code> to the list of imports:
 </p>
 
 <pre>
@@ -386,7 +378,7 @@ import (
        "http"
        "io/ioutil"
        "os"
-       <b>"template"</b>
+       <b>"html/template"</b>
 )
 </pre>
 
@@ -409,7 +401,7 @@ HTML:
 </pre>
 
 <p>
-The function <code>template.ParseFile</code> will read the contents of 
+The function <code>template.ParseFiles</code> will read the contents of 
 <code>edit.html</code> and return a <code>*template.Template</code>. 
 </p>
 
@@ -474,10 +466,11 @@ The handlers are now shorter and simpler.
 <h2>Handling non-existent pages</h2>
 
 <p>
-What if you visit <code>/view/APageThatDoesntExist</code>? The program will 
-crash. This is because it ignores the error return value from
-<code>loadPage</code>. 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 <a href="http://localhost:8080/view/APageThatDoesntExist">
+<code>/view/APageThatDoesntExist</code></a>? The program will crash. This is 
+because it ignores the error return value from <code>loadPage</code>. Instead,
+if the requested Page doesn't exist, it should redirect the client to the edit
+Page so the content may be created:
 </p>
 
 <pre>
@@ -753,9 +746,8 @@ Recompile the code, and run the app:
 </p>
 
 <pre>
-$ 8g wiki.go
-$ 8l wiki.8
-$ ./8.out
+$ go build wiki.go
+$ ./wiki
 </pre>
 
 <p>