

package main
import (
"fmt"
"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("/", indexHandler)
r.HandleFunc("/admin", adminHandler)
http.ListenAndServe(":8080", r)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
templates.ExecuteTemplate(w, "index.html", nil)
}
func adminHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "<h1>后台</h1>")
}