<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello</title>
</head>
<body>
<p>Hello {{.}}</p>
</body>
</html>
package main
import (
"fmt"
"html/template"
"net/http"
)
func SayHello(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./hello.tmpl")
if err != nil {
fmt.Println("解析模板错误:", err)
return
}
err = t.Execute(w, "周靖")
if err != nil {
fmt.Println("渲染模板错误:", err)
return
}
}
func main() {
http.HandleFunc("/", SayHello)
err := http.ListenAndServe(":8000", nil)
if err != nil {
fmt.Println("Http Service Start Failed:", err)
return
}
}