// Homework 34, problem 1: A profile page. // // Define a struct with the exported fields Name, City and Level. In handleHome, // fill one with your own values, and pass it to the template. Finish the // template so that it shows all three. // // Run with: go run exercises/34/homework/01-profile.go package main import ( "bytes" "fmt" "html/template" "net/http" ) // TODO: define the struct here. // TODO: show the three fields in this template. const profileText = `
TODO: replace this line
` var profileTemplate = template.Must(template.New("profile").Parse(profileText)) func handleHome(w http.ResponseWriter, r *http.Request) { // TODO: pass your struct here, in place of nil. var page bytes.Buffer err := profileTemplate.Execute(&page, nil) if err != nil { fmt.Println("Template error:", err) http.Error(w, "The page could not be built.", http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") page.WriteTo(w) } func main() { http.HandleFunc("GET /{$}", handleHome) 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) } }