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>

猜你喜欢

转载自www.cnblogs.com/cheungxiongwei/p/9100326.html
今日推荐