Browse Source

example: use io instead of fmt; add error handling.

Dmitry Chestnykh 14 years ago
parent
commit
0052db45f8
1 changed files with 7 additions and 4 deletions
  1. 7 4
      example/main.go

+ 7 - 4
example/main.go

@@ -2,9 +2,10 @@
 package main
 package main
 
 
 import (
 import (
-	"fmt"
 	"github.com/dchest/captcha"
 	"github.com/dchest/captcha"
 	"http"
 	"http"
+	"io"
+	"log"
 	"template"
 	"template"
 )
 )
 
 
@@ -25,17 +26,19 @@ func showFormHandler(w http.ResponseWriter, r *http.Request) {
 
 
 func processFormHandler(w http.ResponseWriter, r *http.Request) {
 func processFormHandler(w http.ResponseWriter, r *http.Request) {
 	if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
 	if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
-		fmt.Fprintf(w, "Wrong captcha solution! No robots allowed!")
+		io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
 		return
 		return
 	}
 	}
-	fmt.Fprintf(w, "Great job, human! You solved the captcha.")
+	io.WriteString(w, "Great job, human! You solved the captcha.\n")
 }
 }
 
 
 func main() {
 func main() {
 	http.HandleFunc("/", showFormHandler)
 	http.HandleFunc("/", showFormHandler)
 	http.HandleFunc("/process", processFormHandler)
 	http.HandleFunc("/process", processFormHandler)
 	http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
 	http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
-	http.ListenAndServe(":8080", nil)
+	if err := http.ListenAndServe(":8080", nil); err != nil {
+		log.Fatal(err)
+	}
 }
 }
 
 
 const formJavaScript = `
 const formJavaScript = `