模板語法with關鍵字type u struct { Name string Age int Sex string } user:=&u{Name:"asd",Age:20,Sex:"mal"} data["user"]=user c.HTML(http.StatusOK, "index.html"... ...
模板語法
with關鍵字
type u struct {
Name string
Age int
Sex string
}
user:=&u{Name:"asd",Age:20,Sex:"mal"}
data["user"]=user
c.HTML(http.StatusOK, "index.html", data)
輸出
<div>
{{with .user}}
{{.Name}};
{{.Age}};
{{.Sex}}
{{end}}
</div>
迴圈列印數組
nums := []int{1,2,3,4,5,6,7,8,9,0}
data["nums"]=nums
輸出
<div>
{{range .nums}}
{{.}}
{{end}}
</div>
高級使用-模板變數
data["abc"] = "helloword-abc"
c.HTML(http.StatusOK, "index.html", data)
模板變數示例
<div>
{{$tempabc := .abc}}
{{$tempabc}}
</div>