9.8 模板文件

➜  recipe08 cat template.tpl
<html>
    <body>
        Hi, I'm {{.}}!
    </body>
</html>
package main

import "net/http"
import "html/template"
import "fmt"

func main() {
	fmt.Println("Server is starting...")
	tpl, err := template.ParseFiles("template.tpl")
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		err := tpl.Execute(w, "John Doe")
		if err != nil {
			panic(err)
		}
	})
	if err := http.ListenAndServe(":8080", nil); err != nil {
		panic(err)
	}
}

posted on 2018-03-27 00:10  cucy_to  阅读(107)  评论(0编辑  收藏  举报

导航