-<h1>Editing {title}</h1>
+<h1>Editing {Title}</h1>
-<form action="/save/{title}" method="POST">
-<div><textarea name="body" rows="20" cols="80">{body|html}</textarea></div>
+<form action="/save/{Title}" method="POST">
+<div><textarea name="Body" rows="20" cols="80">{Body|html}</textarea></div>
<div><input type="submit" value="Save"></div>
</form>
"template"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
}
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
return
}
body := r.FormValue("body")
- p := &page{title: title, body: []byte(body)}
+ p := &Page{Title: title, Body: []byte(body)}
err = p.save()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
-func renderTemplate(w http.ResponseWriter, 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(w, err.String(), http.StatusInternalServerError)
"template"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
const lenPath = len("/view/")
title := r.URL.Path[lenPath:]
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
t, _ := template.ParseFile("edit.html", nil)
t.Execute(p, w)
"template"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
func viewHandler(w http.ResponseWriter, 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}
+ p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
- p := &page{title: title, body: []byte(body)}
+ p := &Page{Title: title, Body: []byte(body)}
err := p.save()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
-func renderTemplate(w http.ResponseWriter, 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(w, err.String(), http.StatusInternalServerError)
"template"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
const lenPath = len("/view/")
title := r.URL.Path[lenPath:]
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
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 := &Page{Title: title, Body: []byte(body)}
p.save()
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
-func renderTemplate(w http.ResponseWriter, tmpl string, p *page) {
+func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
t, _ := template.ParseFile(tmpl+".html", nil)
t.Execute(p, w)
}
"template"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
func viewHandler(w http.ResponseWriter, 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}
+ p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
- p := &page{title: title, body: []byte(body)}
+ p := &Page{Title: title, Body: []byte(body)}
err := p.save()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}
-func renderTemplate(w http.ResponseWriter, tmpl string, p *page) {
+func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates[tmpl].Execute(p, w)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
func main() {
b, _ := ioutil.ReadAll(os.Stdin)
- template.HTMLFormatter(os.Stdout, b, "")
+ template.HTMLFormatter(os.Stdout, "", b)
}
<p>
Let's start by defining the data structures. A wiki consists of a series of
interconnected pages, each of which has a title and a body (the page content).
-Here, we define <code>page</code> as a struct with two fields representing
+Here, we define <code>Page</code> as a struct with two fields representing
the title and body.
</p>
<pre>
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
</pre>
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.)
-The <code>body</code> element is a <code>[]byte</code> rather than
+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.
</p>
<p>
-The <code>page</code> struct describes how page data will be stored in memory.
+The <code>Page</code> struct describes how page data will be stored in memory.
But what about persistent storage? We can address that by creating a
-<code>save</code> method on <code>page</code>:
+<code>save</code> method on <code>Page</code>:
</p>
<pre>
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
</pre>
<p>
This method's signature reads: "This is a method named <code>save</code> that
-takes as its receiver <code>p</code>, a pointer to <code>page</code> . It takes
+takes as its receiver <code>p</code>, a pointer to <code>Page</code> . It takes
no parameters, and returns a value of type <code>os.Error</code>."
</p>
<p>
-This method will save the <code>page</code>'s <code>body</code> to a text
-file. For simplicity, we will use the <code>title</code> as the file name.
+This method will save the <code>Page</code>'s <code>Body</code> to a text
+file. For simplicity, we will use the <code>Title</code> as the file name.
</p>
<p>
that is the return type of <code>WriteFile</code> (a standard library function
that writes a byte slice to a file). The <code>save</code> method returns the
error value, to let the application handle it should anything go wrong while
-writing the file. If all goes well, <code>page.save()</code> will return
+writing the file. If all goes well, <code>Page.save()</code> will return
<code>nil</code> (the zero-value for pointers, interfaces, and some other
types).
</p>
</p>
<pre>
-func loadPage(title string) *page {
+func loadPage(title string) *Page {
filename := title + ".txt"
body, _ := ioutil.ReadFile(filename)
- return &page{title: title, body: body}
+ return &Page{Title: title, Body: body}
}
</pre>
<p>
The function <code>loadPage</code> constructs the file name from
-<code>title</code>, reads the file's contents into a new
-<code>page</code>, and returns a pointer to that new <code>page</code>.
+<code>Title</code>, reads the file's contents into a new
+<code>Page</code>, and returns a pointer to that new <code>page</code>.
</p>
<p>
<p>
But what happens if <code>ReadFile</code> encounters an error? For example,
the file might not exist. We should not ignore such errors. Let's modify the
-function to return <code>*page</code> and <code>os.Error</code>.
+function to return <code>*Page</code> and <code>os.Error</code>.
</p>
<pre>
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
</pre>
<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>nil</code> then it has successfully loaded a Page. If not, it will be an
<code>os.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).
<pre>
func main() {
- p1 := &page{title: "TestPage", body: []byte("This is a sample page.")}
+ p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
p1.save()
p2, _ := loadPage("TestPage")
- fmt.Println(string(p2.body))
+ fmt.Println(string(p2.Body))
}
</pre>
<p>
After compiling and executing this code, a file named <code>TestPage.txt</code>
would be created, containing the contents of <code>p1</code>. The file would
-then be read into the struct <code>p2</code>, and its <code>body</code> element
+then be read into the struct <code>p2</code>, and its <code>Body</code> element
printed to the screen.
</p>
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
p, _ := loadPage(title)
- fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.title, p.body)
+ fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
</pre>
should show a page titled "test" containing the words "Hello world".
</p>
-<h2>Editing pages</h2>
+<h2>Editing Pages</h2>
<p>
A wiki is not a wiki without the ability to edit pages. Let's create two new
<p>
The function <code>editHandler</code> loads the page
-(or, if it doesn't exist, create an empty <code>page</code> struct),
+(or, if it doesn't exist, create an empty <code>Page</code> struct),
and displays an HTML form.
</p>
title := r.URL.Path[lenPath:]
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
fmt.Fprintf(w, "<h1>Editing %s</h1>"+
"<form action=\"/save/%s\" method=\"POST\">"+
"<textarea name=\"body\">%s</textarea><br>"+
"<input type=\"submit\" value=\"Save\">"+
"</form>",
- p.title, p.title, p.body)
+ p.Title, p.Title, p.Body)
}
</pre>
</p>
<pre>
-<h1>Editing {title}</h1>
+<h1>Editing {Title}</h1>
-<form action="/save/{title}" method="POST">
-<div><textarea name="body" rows="20" cols="80">{body|html}</textarea></div>
+<form action="/save/{Title}" method="POST">
+<div><textarea name="Body" rows="20" cols="80">{Body|html}</textarea></div>
<div><input type="submit" value="Save"></div>
</form>
</pre>
title := r.URL.Path[lenPath:]
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
t, _ := template.ParseFile("edit.html", nil)
t.Execute(p, w)
<p>
The method <code>t.Execute</code> replaces all occurrences of
-<code>{title}</code> and <code>{body}</code> with the values of
-<code>p.title</code> and <code>p.body</code>, and writes the resultant
+<code>{Title}</code> and <code>{Body}</code> with the values of
+<code>p.Title</code> and <code>p.Body</code>, and writes the resultant
HTML to the <code>http.ResponseWriter</code>.
</p>
<p>
-Note that we've used <code>{body|html}</code> in the above template.
+Note that we've used <code>{Body|html}</code> in the above template.
The <code>|html</code> part asks the template engine to pass the value
-<code>body</code> through the <code>html</code> formatter before outputting it,
+<code>Body</code> through the <code>html</code> formatter before outputting it,
which escapes HTML characters (such as replacing <code>></code> with
<code>&gt;</code>).
This will prevent user data from corrupting the form HTML.
</p>
<pre>
-<h1>{title}</h1>
+<h1>{Title}</h1>
-<p>[<a href="/edit/{title}">edit</a>]</p>
+<p>[<a href="/edit/{Title}">edit</a>]</p>
-<div>{body}</div>
+<div>{Body}</div>
</pre>
<p>
title := r.URL.Path[lenPath:]
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
-func renderTemplate(w http.ResponseWriter, tmpl string, p *page) {
+func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
t, _ := template.ParseFile(tmpl+".html", nil)
t.Execute(p, w)
}
<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:
+<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>
header to the HTTP response.
</p>
-<h2>Saving pages</h2>
+<h2>Saving Pages</h2>
<p>
The function <code>saveHandler</code> will handle the form submission.
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 := &Page{Title: title, Body: []byte(body)}
p.save()
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
<p>
The page title (provided in the URL) and the form's only field,
-<code>body</code>, are stored in a new <code>page</code>.
+<code>Body</code>, are stored in a new <code>Page</code>.
The <code>save()</code> method is then called to write the data to a file,
and the client is redirected to the <code>/view/</code> page.
</p>
<p>
The value returned by <code>FormValue</code> is of type <code>string</code>.
We must convert that value to <code>[]byte</code> before it will fit into
-the <code>page</code> struct. We use <code>[]byte(body)</code> to perform
+the <code>Page</code> struct. We use <code>[]byte(body)</code> to perform
the conversion.
</p>
</p>
<pre>
-func renderTemplate(w http.ResponseWriter, 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(w, err.String(), http.StatusInternalServerError)
<pre>
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
- p := &page{title: title, body: []byte(body)}
+ p := &Page{Title: title, Body: []byte(body)}
err := p.save()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
<code>templates</code>:
<pre>
-func renderTemplate(w http.ResponseWriter, tmpl string, p *page) {
+func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates[tmpl].Execute(p, w)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
</p>
<pre>
-var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
</pre>
<p>
<p>
Now, let's write a function that extracts the title string from the request
-URL, and tests it against our <code>titleValidator</code> expression:
+URL, and tests it against our <code>TitleValidator</code> expression:
</p>
<pre>
}
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
return
}
body := r.FormValue("body")
- p := &page{title: title, body: []byte(body)}
+ p := &Page{Title: title, Body: []byte(body)}
err = p.save()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
an <code>http.ResponseWriter</code> and <code>http.Request</code> (in other
words, an <code>http.HandlerFunc</code>).
The closure extracts the <code>title</code> from the request path, and
-validates it with the <code>titleValidator</code> regexp. If the
+validates it with the <code>TitleValidator</code> regexp. If the
<code>title</code> is invalid, an error will be written to the
<code>ResponseWriter</code> using the <code>http.NotFound</code> function.
If the <code>title</code> is valid, the enclosed handler function
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
- p := &page{title: title, body: []byte(body)}
+ p := &Page{Title: title, Body: []byte(body)}
err := p.save()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
"os"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
const lenPath = len("/view/")
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
p, _ := loadPage(title)
- fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.title, p.body)
+ fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
p, err := loadPage(title)
if err != nil {
- p = &page{title: title}
+ p = &Page{Title: title}
}
fmt.Fprintf(w, "<h1>Editing %s</h1>"+
"<form action=\"/save/%s\" method=\"POST\">"+
"<textarea name=\"body\">%s</textarea><br>"+
"<input type=\"submit\" value=\"Save\">"+
"</form>",
- p.title, p.title, p.body)
+ p.Title, p.Title, p.Body)
}
func main() {
"os"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) *page {
+func loadPage(title string) *Page {
filename := title + ".txt"
body, _ := ioutil.ReadFile(filename)
- return &page{title: title, body: body}
+ return &Page{Title: title, Body: body}
}
func main() {
- p1 := &page{title: "TestPage", body: []byte("This is a sample page.")}
+ p1 := &Page{Title: "TestPage", Body: []byte("This is a sample page.")}
p1.save()
p2 := loadPage("TestPage")
- fmt.Println(string(p2.body))
+ fmt.Println(string(p2.Body))
}
"os"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
func main() {
- p1 := &page{title: "TestPage", body: []byte("This is a sample page.")}
+ p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
p1.save()
p2, _ := loadPage("TestPage")
- fmt.Println(string(p2.body))
+ fmt.Println(string(p2.Body))
}
"os"
)
-type page struct {
- title string
- body []byte
+type Page struct {
+ Title string
+ Body []byte
}
-func (p *page) save() os.Error {
- filename := p.title + ".txt"
- return ioutil.WriteFile(filename, p.body, 0600)
+func (p *Page) save() os.Error {
+ filename := p.Title + ".txt"
+ return ioutil.WriteFile(filename, p.Body, 0600)
}
-func loadPage(title string) (*page, os.Error) {
+func loadPage(title string) (*Page, os.Error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- return &page{title: title, body: body}, nil
+ return &Page{Title: title, Body: body}, nil
}
const lenPath = len("/view/")
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
p, _ := loadPage(title)
- fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.title, p.body)
+ fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
func main() {
"go/parser"
"go/printer"
"go/ast"
+ "go/token"
"log"
"os"
)
os.Exit(2)
}
// load file
- file, err := parser.ParseFile(*srcFn, nil, 0)
+ fs := token.NewFileSet()
+ file, err := parser.ParseFile(fs, *srcFn, nil, 0)
if err != nil {
log.Exit(err)
}
os.Exit(1)
}
b := new(bytes.Buffer)
- p.Fprint(b, file)
+ p.Fprint(b, fs, file)
// drop package declaration
if !*showPkg {
for {
<h1>Editing Test</h1>
<form action="/save/Test" method="POST">
-<div><textarea name="body" rows="20" cols="80"></textarea></div>
+<div><textarea name="Body" rows="20" cols="80"></textarea></div>
<div><input type="submit" value="Save"></div>
</form>
-<h1>{title}</h1>
+<h1>{Title}</h1>
-<p>[<a href="/edit/{title}">edit</a>]</p>
+<p>[<a href="/edit/{Title}">edit</a>]</p>
-<div>{body}</div>
+<div>{Body}</div>
<p>
Let's start by defining the data structures. A wiki consists of a series of
interconnected pages, each of which has a title and a body (the page content).
-Here, we define <code>page</code> as a struct with two fields representing
+Here, we define <code>Page</code> as a struct with two fields representing
the title and body.
</p>
<pre>
-!./srcextract.bin -src=part1.go -name=page
+!./srcextract.bin -src=part1.go -name=Page
</pre>
<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.)
-The <code>body</code> element is a <code>[]byte</code> rather than
+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.
</p>
<p>
-The <code>page</code> struct describes how page data will be stored in memory.
+The <code>Page</code> struct describes how page data will be stored in memory.
But what about persistent storage? We can address that by creating a
-<code>save</code> method on <code>page</code>:
+<code>save</code> method on <code>Page</code>:
</p>
<pre>
<p>
This method's signature reads: "This is a method named <code>save</code> that
-takes as its receiver <code>p</code>, a pointer to <code>page</code> . It takes
+takes as its receiver <code>p</code>, a pointer to <code>Page</code> . It takes
no parameters, and returns a value of type <code>os.Error</code>."
</p>
<p>
-This method will save the <code>page</code>'s <code>body</code> to a text
-file. For simplicity, we will use the <code>title</code> as the file name.
+This method will save the <code>Page</code>'s <code>Body</code> to a text
+file. For simplicity, we will use the <code>Title</code> as the file name.
</p>
<p>
that is the return type of <code>WriteFile</code> (a standard library function
that writes a byte slice to a file). The <code>save</code> method returns the
error value, to let the application handle it should anything go wrong while
-writing the file. If all goes well, <code>page.save()</code> will return
+writing the file. If all goes well, <code>Page.save()</code> will return
<code>nil</code> (the zero-value for pointers, interfaces, and some other
types).
</p>
<p>
The function <code>loadPage</code> constructs the file name from
-<code>title</code>, reads the file's contents into a new
-<code>page</code>, and returns a pointer to that new <code>page</code>.
+<code>Title</code>, reads the file's contents into a new
+<code>Page</code>, and returns a pointer to that new <code>page</code>.
</p>
<p>
<p>
But what happens if <code>ReadFile</code> encounters an error? For example,
the file might not exist. We should not ignore such errors. Let's modify the
-function to return <code>*page</code> and <code>os.Error</code>.
+function to return <code>*Page</code> and <code>os.Error</code>.
</p>
<pre>
<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>nil</code> then it has successfully loaded a Page. If not, it will be an
<code>os.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).
<p>
After compiling and executing this code, a file named <code>TestPage.txt</code>
would be created, containing the contents of <code>p1</code>. The file would
-then be read into the struct <code>p2</code>, and its <code>body</code> element
+then be read into the struct <code>p2</code>, and its <code>Body</code> element
printed to the screen.
</p>
should show a page titled "test" containing the words "Hello world".
</p>
-<h2>Editing pages</h2>
+<h2>Editing Pages</h2>
<p>
A wiki is not a wiki without the ability to edit pages. Let's create two new
<p>
The function <code>editHandler</code> loads the page
-(or, if it doesn't exist, create an empty <code>page</code> struct),
+(or, if it doesn't exist, create an empty <code>Page</code> struct),
and displays an HTML form.
</p>
<p>
The method <code>t.Execute</code> replaces all occurrences of
-<code>{title}</code> and <code>{body}</code> with the values of
-<code>p.title</code> and <code>p.body</code>, and writes the resultant
+<code>{Title}</code> and <code>{Body}</code> with the values of
+<code>p.Title</code> and <code>p.Body</code>, and writes the resultant
HTML to the <code>http.ResponseWriter</code>.
</p>
<p>
-Note that we've used <code>{body|html}</code> in the above template.
+Note that we've used <code>{Body|html}</code> in the above template.
The <code>|html</code> part asks the template engine to pass the value
-<code>body</code> through the <code>html</code> formatter before outputting it,
+<code>Body</code> through the <code>html</code> formatter before outputting it,
which escapes HTML characters (such as replacing <code>></code> with
<code>&gt;</code>).
This will prevent user data from corrupting the form HTML.
<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:
+<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>
header to the HTTP response.
</p>
-<h2>Saving pages</h2>
+<h2>Saving Pages</h2>
<p>
The function <code>saveHandler</code> will handle the form submission.
<p>
The page title (provided in the URL) and the form's only field,
-<code>body</code>, are stored in a new <code>page</code>.
+<code>Body</code>, are stored in a new <code>Page</code>.
The <code>save()</code> method is then called to write the data to a file,
and the client is redirected to the <code>/view/</code> page.
</p>
<p>
The value returned by <code>FormValue</code> is of type <code>string</code>.
We must convert that value to <code>[]byte</code> before it will fit into
-the <code>page</code> struct. We use <code>[]byte(body)</code> to perform
+the <code>Page</code> struct. We use <code>[]byte(body)</code> to perform
the conversion.
</p>
</p>
<pre>
-!./srcextract.bin -src=final-noclosure.go -name=titleValidator
+!./srcextract.bin -src=final-noclosure.go -name=TitleValidator
</pre>
<p>
<p>
Now, let's write a function that extracts the title string from the request
-URL, and tests it against our <code>titleValidator</code> expression:
+URL, and tests it against our <code>TitleValidator</code> expression:
</p>
<pre>
an <code>http.ResponseWriter</code> and <code>http.Request</code> (in other
words, an <code>http.HandlerFunc</code>).
The closure extracts the <code>title</code> from the request path, and
-validates it with the <code>titleValidator</code> regexp. If the
+validates it with the <code>TitleValidator</code> regexp. If the
<code>title</code> is invalid, an error will be written to the
<code>ResponseWriter</code> using the <code>http.NotFound</code> function.
If the <code>title</code> is valid, the enclosed handler function
fi
) || exit $?
+(xcd ../doc/codelab/wiki
+gomake test) || exit $?
+
for i in ../misc/dashboard/builder ../misc/goplay
do
(xcd $i