Commit 44618b28 authored by Alex Schroeder's avatar Alex Schroeder Committed by Andrew Gerrand

wiki article: remove "flag" import from the code

When reading along the article, the extra code added in the final
version is not explained. The main function calls flag.Parse(), for
example, which will cause an error, unless the readers looks at the
entirety of final.go to see the import added.

The file shown to the users no longer has the extra flags. The testing
code is now in a patch that gets applied to final.go in order to create
final-test.go. This is the file that will be used to test the code,
matching final.go as much as possible.

Change-Id: I022f5f6c88e107c8ba5623661d74a8d260d05266
Reviewed-on: https://go-review.googlesource.com/11061Reviewed-by: 's avatarAndrew Gerrand <adg@golang.org>
parent 202de394
*** final.go 2015-06-14 23:59:22.000000000 +0200
--- final-test.go 2015-06-15 00:15:41.000000000 +0200
***************
*** 7,12 ****
--- 7,14 ----
import (
"html/template"
"io/ioutil"
+ "log"
+ "net"
"net/http"
"regexp"
)
***************
*** 85,89 ****
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
! http.ListenAndServe(":8080", nil)
}
--- 87,101 ----
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
! l, err := net.Listen("tcp", "127.0.0.1:0")
! if err != nil {
! log.Fatal(err)
! }
! err = ioutil.WriteFile("final-test-port.txt", []byte(l.Addr().String()), 0644)
! if err != nil {
! log.Fatal(err)
! }
! s := &http.Server{}
! s.Serve(l)
! return
}
...@@ -5,19 +5,12 @@ ...@@ -5,19 +5,12 @@
package main package main
import ( import (
"flag"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net"
"net/http" "net/http"
"regexp" "regexp"
) )
var (
addr = flag.Bool("addr", false, "find open address and print to final-port.txt")
)
type Page struct { type Page struct {
Title string Title string
Body []byte Body []byte
...@@ -88,24 +81,9 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl ...@@ -88,24 +81,9 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl
} }
func main() { func main() {
flag.Parse()
http.HandleFunc("/view/", makeHandler(viewHandler)) http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler)) http.HandleFunc("/save/", makeHandler(saveHandler))
if *addr {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile("final-port.txt", []byte(l.Addr().String()), 0644)
if err != nil {
log.Fatal(err)
}
s := &http.Server{}
s.Serve(l)
return
}
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }
...@@ -7,11 +7,11 @@ set -e ...@@ -7,11 +7,11 @@ set -e
wiki_pid= wiki_pid=
cleanup() { cleanup() {
kill $wiki_pid kill $wiki_pid
rm -f test_*.out Test.txt final.bin final-port.txt a.out get.bin rm -f test_*.out Test.txt final-test.bin final-test-port.txt a.out get.bin
} }
trap cleanup 0 INT trap cleanup 0 INT
rm -f get.bin final.bin a.out rm -f get.bin final-test.bin a.out
# If called with -all, check that all code snippets compile. # If called with -all, check that all code snippets compile.
if [ "$1" == "-all" ]; then if [ "$1" == "-all" ]; then
...@@ -21,12 +21,14 @@ if [ "$1" == "-all" ]; then ...@@ -21,12 +21,14 @@ if [ "$1" == "-all" ]; then
fi fi
go build -o get.bin get.go go build -o get.bin get.go
go build -o final.bin final.go cp final.go final-test.go
(./final.bin --addr) & patch final-test.go final-test.patch > /dev/null
go build -o final-test.bin final-test.go
./final-test.bin &
wiki_pid=$! wiki_pid=$!
l=0 l=0
while [ ! -f ./final-port.txt ] while [ ! -f ./final-test-port.txt ]
do do
l=$(($l+1)) l=$(($l+1))
if [ "$l" -gt 5 ] if [ "$l" -gt 5 ]
...@@ -38,7 +40,7 @@ do ...@@ -38,7 +40,7 @@ do
sleep 1 sleep 1
done done
addr=$(cat final-port.txt) addr=$(cat final-test-port.txt)
./get.bin http://$addr/edit/Test > test_edit.out ./get.bin http://$addr/edit/Test > test_edit.out
diff -u test_edit.out test_edit.good diff -u test_edit.out test_edit.good
./get.bin -post=body=some%20content http://$addr/save/Test > test_save.out ./get.bin -post=body=some%20content http://$addr/save/Test > test_save.out
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment