package main import ( "bytes" "fmt" "html/template" "net/http" ) type profile struct { Name string City string Level int } const profileText = ` Profile of {{.Name}}

{{.Name}}

City: {{.City}}

Level: {{.Level}}

` var profileTemplate = template.Must(template.New("profile").Parse(profileText)) func handleHome(w http.ResponseWriter, r *http.Request) { data := profile{Name: "Keiko", City: "Bandung", Level: 7} var page bytes.Buffer err := profileTemplate.Execute(&page, data) 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) } }