// Chapter 32 example: replying with HTML from a string. // Run with: go run exercises/32/02-html-page.go package main import ( "fmt" "net/http" ) const homePage = `
This page came from a Go string.
` const aboutPage = `It has two pages.
` func handleHome(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprint(w, homePage) } func handleAbout(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprint(w, aboutPage) } func main() { http.HandleFunc("GET /{$}", handleHome) http.HandleFunc("GET /about", handleAbout) 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) } }