package main
import("net/http""html/template""time")funcwelcome(w http.ResponseWriter, r *http.Request){
t,_:= template.ParseFiles("view/index.html")
time:=time.Date(2018,1,2,2,4,5,0,time.Local)
t.Execute(w, time)//此处传递数据}funcmain(){
server := http.Server{Addr:":8090"}
http.HandleFunc("/", welcome)
server.ListenAndServe()}
html代码如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Title</title></head><body><pre><!--调用time变量的无参方法-->
取出时间中的年:{{.Year}} <br/>
取出时间中的年:{{.Month}} <br/><!--调用有参数方法-->
格式化后的内容:{{.Format "2006-01-02"}}
</pre></body></html>