OLD | NEW |
1 <div class="content"> | 1 <div class="content"> |
2 | 2 |
3 <h1>Writing Web Applications</h1> | 3 <h1>Writing Web Applications</h1> |
4 | 4 |
5 <h2>Introduction</h2> | 5 <h2>Introduction</h2> |
6 | 6 |
7 <p> | 7 <p> |
8 Covered in this codelab: | 8 Covered in this codelab: |
9 </p> | 9 </p> |
10 <ul> | 10 <ul> |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 body, err := ioutil.ReadFile(filename) | 173 body, err := ioutil.ReadFile(filename) |
174 if err != nil { | 174 if err != nil { |
175 return nil, err | 175 return nil, err |
176 } | 176 } |
177 return &page{title: title, body: body}, nil | 177 return &page{title: title, body: body}, nil |
178 } | 178 } |
179 </pre> | 179 </pre> |
180 | 180 |
181 <p> | 181 <p> |
182 Callers of this function can now check the second parameter; if it is | 182 Callers of this function can now check the second parameter; if it is |
183 <code>nil</code> then it has succesfully loaded a page. If not, it will be an | 183 <code>nil</code> then it has successfully loaded a page. If not, it will be an |
184 <code>os.Error</code> that can be handled by the caller (see the <a | 184 <code>os.Error</code> that can be handled by the caller (see the <a |
185 href="http://golang.org/pkg/os/#Error">os package documentation</a> for· | 185 href="http://golang.org/pkg/os/#Error">os package documentation</a> for· |
186 details). | 186 details). |
187 </p> | 187 </p> |
188 | 188 |
189 <p> | 189 <p> |
190 At this point we have a simple data structure and the ability to save to and | 190 At this point we have a simple data structure and the ability to save to and |
191 load from a file. Let's write a <code>main</code> function to test what we've | 191 load from a file. Let's write a <code>main</code> function to test what we've |
192 written: | 192 written: |
193 </p> | 193 </p> |
(...skipping 21 matching lines...) Expand all Loading... |
215 <pre> | 215 <pre> |
216 $ 8g wiki.go | 216 $ 8g wiki.go |
217 $ 8l wiki.8 | 217 $ 8l wiki.8 |
218 $ ./8.out | 218 $ ./8.out |
219 This is a sample page. | 219 This is a sample page. |
220 </pre> | 220 </pre> |
221 | 221 |
222 <p> | 222 <p> |
223 (The <code>8g</code> and <code>8l</code> commands are applicable to | 223 (The <code>8g</code> and <code>8l</code> commands are applicable to |
224 <code>GOARCH=386</code>. If you're on an <code>amd64</code> system, | 224 <code>GOARCH=386</code>. If you're on an <code>amd64</code> system, |
225 subtitute 6's for the 8's.) | 225 substitute 6's for the 8's.) |
226 </p> | 226 </p> |
227 | 227 |
228 <p> | 228 <p> |
229 <a href="part1.go">Click here to view the code we've written so far.</a> | 229 <a href="part1.go">Click here to view the code we've written so far.</a> |
230 </p> | 230 </p> |
231 | 231 |
232 <h2>Introducing the <code>http</code> package (an interlude)</h2> | 232 <h2>Introducing the <code>http</code> package (an interlude)</h2> |
233 | 233 |
234 <p> | 234 <p> |
235 Here's a full working example of a simple web server: | 235 Here's a full working example of a simple web server: |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 <pre> | 445 <pre> |
446 import ( | 446 import ( |
447 "http" | 447 "http" |
448 "io/ioutil" | 448 "io/ioutil" |
449 "os" | 449 "os" |
450 <b>"template"</b> | 450 <b>"template"</b> |
451 ) | 451 ) |
452 </pre> | 452 </pre> |
453 | 453 |
454 <p> | 454 <p> |
455 Let's create a template file containg the HTML form.· | 455 Let's create a template file containing the HTML form.· |
456 Open a new file named <code>edit.html</code>, and add the following lines: | 456 Open a new file named <code>edit.html</code>, and add the following lines: |
457 </p> | 457 </p> |
458 | 458 |
459 <pre> | 459 <pre> |
460 <h1>Editing {title}</h1> | 460 <h1>Editing {title}</h1> |
461 | 461 |
462 <form action="/save/{title}" method="POST"> | 462 <form action="/save/{title}" method="POST"> |
463 <div><textarea name="body" rows="20" cols="80"&
gt;{body|html}</textarea></div> | 463 <div><textarea name="body" rows="20" cols="80"&
gt;{body|html}</textarea></div> |
464 <div><input type="submit" value="Save"></div> | 464 <div><input type="submit" value="Save"></div> |
465 </form> | 465 </form> |
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 <li>Spruce up the page templates by making them valid HTML and adding some | 991 <li>Spruce up the page templates by making them valid HTML and adding some |
992 CSS rules.</li> | 992 CSS rules.</li> |
993 <li>Implement inter-page linking by converting instances of· | 993 <li>Implement inter-page linking by converting instances of· |
994 <code>[PageName]</code> to <br> | 994 <code>[PageName]</code> to <br> |
995 <code><a href="/view/PageName">PageName</a></code>. | 995 <code><a href="/view/PageName">PageName</a></code>. |
996 (hint: you could use <code>regexp.ReplaceAllFunc</code> to do this) | 996 (hint: you could use <code>regexp.ReplaceAllFunc</code> to do this) |
997 </li> | 997 </li> |
998 </ul> | 998 </ul> |
999 | 999 |
1000 </div> | 1000 </div> |
OLD | NEW |