package main
import (
"github.com/gorilla/mux"
"html/template" //模板包
"net/http"
)
var (
//申明模板变量,自动引入模板包
templates *template.Template
)
func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
//普通路由
r.HandleFunc("/form", formHandler)
r.HandleFunc("/formadd", addFromHandler)
http.ListenAndServe(":8080", r)
}
func formHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
templates.ExecuteTemplate(w, "form.html", nil)
}
func addFromHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
r.ParseForm()
comment := r.PostForm.Get("content")
templates.ExecuteTemplate(w, "form.html", comment)
}