golang 如何使用模版?

package main

import (
	"fmt"
	"net/http"
	"log"
	"html/template"
)

func main ()  {
	//实例化一个 HTTP
	app := http.NewServeMux();
	app.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){
		switch r.Method {
		case "GET":
			tmpl,_ := template.ParseFiles("./View/home.html");
			tmpl.Execute(w,"Master");
		case "POST":
		case "PUT":
		case "DELETE":
		default:
		}
	});
	address := "127.0.0.1:80"
	fmt.Printf("Application: running using host(http://%s) \n",address);
	log.Fatal(http.ListenAndServe(address,app));
}

View/home.html

<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <center>
            <h1>Hello,My name is {{.}}</h1>
        </center>
    </body>
</html>
posted @ 2018-05-28 15:17  學海無涯  阅读(324)  评论(0编辑  收藏  举报