-
Notifications
You must be signed in to change notification settings - Fork 0
golang http template simple parse
pikachule edited this page Aug 6, 2017
·
1 revision
server.go
package main
import (
"html/template"
"log"
"net/http"
"github.com/gorilla/mux"
)
func HomepageHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./tpl.html")
data := struct {
Title string
}{Title: "golang html template demo"}
err = t.Execute(w, data)
if err != nil {
log.Fatal(err)
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomepageHandler)
log.Fatal(http.ListenAndServe(":1234", r))
}
tpl.html
<body>
<h1>{{.Title}}</h1>
</body>