]> Cypherpunks repositories - gostls13.git/commitdiff
wiki article: remove "flag" import from the code
authorAlex Schroeder <alex@gnu.org>
Sun, 14 Jun 2015 10:54:05 +0000 (12:54 +0200)
committerAndrew Gerrand <adg@golang.org>
Sun, 14 Jun 2015 23:26:54 +0000 (23:26 +0000)
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/11061
Reviewed-by: Andrew Gerrand <adg@golang.org>
doc/articles/wiki/final-test.patch [new file with mode: 0644]
doc/articles/wiki/final.go
doc/articles/wiki/test.bash

diff --git a/doc/articles/wiki/final-test.patch b/doc/articles/wiki/final-test.patch
new file mode 100644 (file)
index 0000000..499ad78
--- /dev/null
@@ -0,0 +1,36 @@
+*** 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
+  }
index d84c1ffb264663a279a76554c3e22d07009b22b9..139a323010830e7100b86002bcd866fb15f3f752 100644 (file)
@@ -5,19 +5,12 @@
 package main
 
 import (
-       "flag"
        "html/template"
        "io/ioutil"
-       "log"
-       "net"
        "net/http"
        "regexp"
 )
 
-var (
-       addr = flag.Bool("addr", false, "find open address and print to final-port.txt")
-)
-
 type Page struct {
        Title string
        Body  []byte
@@ -88,24 +81,9 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl
 }
 
 func main() {
-       flag.Parse()
        http.HandleFunc("/view/", makeHandler(viewHandler))
        http.HandleFunc("/edit/", makeHandler(editHandler))
        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)
 }
index 2997f1680a6aa08f79b21113c0c08d718b531a88..8ecd666da348ce6089010f710473795f1da42e88 100755 (executable)
@@ -7,11 +7,11 @@ set -e
 wiki_pid=
 cleanup() {
        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
 
-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 [ "$1" == "-all" ]; then
@@ -21,12 +21,14 @@ if [ "$1" == "-all" ]; then
 fi
 
 go build -o get.bin get.go
-go build -o final.bin final.go
-(./final.bin --addr) &
+cp final.go final-test.go
+patch final-test.go final-test.patch > /dev/null
+go build -o final-test.bin final-test.go
+./final-test.bin &
 wiki_pid=$!
 
 l=0
-while [ ! -f ./final-port.txt ]
+while [ ! -f ./final-test-port.txt ]
 do
        l=$(($l+1))
        if [ "$l" -gt 5 ]
@@ -38,7 +40,7 @@ do
        sleep 1
 done
 
-addr=$(cat final-port.txt)
+addr=$(cat final-test-port.txt)
 ./get.bin http://$addr/edit/Test > test_edit.out
 diff -u test_edit.out test_edit.good
 ./get.bin -post=body=some%20content http://$addr/save/Test > test_save.out