zoukankan      html  css  js  c++  java
  • 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>
    

    处理简单数据-模板函数

  • 相关阅读:
    1004. Counting Leaves (30)
    51Nod 1272 最大距离 (栈或贪心)
    D
    M
    N
    F
    E
    L
    A. Office Keys ( Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) )
    K
  • 原文地址:https://www.cnblogs.com/daoyi/p/golang-mo-ban-yu-fa.html
Copyright © 2011-2022 走看看