index.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package main
import (
"log"
"net/http"
"html/template"
)
type Page struct {
Title string
Body []byte
}
var templates = template.Must(template.ParseFiles("index.html"))
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
p := &Page{Title: "index"}
renderTemplate(w, "index", p)
}
func main() {
http.HandleFunc("/", viewHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
|