render html in default route

This commit is contained in:
talksik
2025-11-09 13:42:43 -08:00
parent a3f90562d5
commit d0fd2be150
+24 -27
View File
@@ -1,51 +1,48 @@
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
)
var x = 2
func testTemplate() {
const tpl = `
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
// fmt.Fprintf(w, "Welcome to the home page!")
const tpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body>
{{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
{{range .Items}}<div class="bg-red-200">{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
</body>
</html>`
t, err := template.New("default").Parse(tpl)
if err != nil {
log.Fatalf("%v", err)
}
t, err := template.New("default").Parse(tpl)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Title string
Items []string
}{
Title: "Hello",
Items: []string{"yo", template.HTMLEscapeString("what's up")},
}
err = t.Execute(os.Stdout, data)
if err != nil {
log.Fatalf("%v", err)
}
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "Welcome to the home page!")
data := struct {
Title string
Items []string
}{
Title: "Hello",
Items: []string{"yo", "what's up"}, // template automatically escapes it
}
err = t.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
log.Printf("listening on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))