package main import ( "fmt" "net/http" "strconv" ) const formPage = ` Rate this book

Rate this book

` func handleForm(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprint(w, formPage) } func handleRating(w http.ResponseWriter, r *http.Request) { rating, err := strconv.Atoi(r.FormValue("rating")) if err != nil { http.Error(w, "The rating must be a whole number.", http.StatusBadRequest) return } if rating < 1 || rating > 5 { http.Error(w, "The rating must be from 1 to 5.", http.StatusBadRequest) return } fmt.Println("Rating received:", rating) http.Redirect(w, r, "/thanks", http.StatusSeeOther) } func handleThanks(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Thank you for your rating.") } func main() { http.HandleFunc("GET /{$}", handleForm) http.HandleFunc("POST /rating", handleRating) http.HandleFunc("GET /thanks", handleThanks) fmt.Println("Server running at http://localhost:8080") fmt.Println("Press Ctrl+C to stop it.") err := http.ListenAndServe("localhost:8080", nil) if err != nil { fmt.Println("Server stopped:", err) } }