From: Shenghou Ma
http
package to build web applications
-template
package to process HTML templatesnet/http
package to build web applications
+html/template
package to process HTML templatesregexp
package to validate user input
-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.
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.
-http
package (an interlude)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!-
http
to serve wiki pagesnet/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.
-template
packagehtml/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
.
-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 inrenderTemplate
: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 +$ ./wikidiff --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:
http
package to build web applications
-template
package to process HTML templatesnet/http
package to build web applications
+html/template
package to process HTML templatesregexp
package to validate user input
-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.
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.
-http
package (an interlude)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!-
http
to serve wiki pagesnet/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.
-template
packagehtml/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
.
-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