golang模板语法

模板语法

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>

处理简单数据-模板函数

posted @ 2017-04-23 01:37  罗道义  阅读(1693)  评论(0编辑  收藏  举报
本站总访问量