• Go template详解(中)- 变量使用、if语句、迭代(数组、切片、map)、内置函数(比较、逻辑判断、打印、索引、函数调用)


    5. 变量

    5.1 变量使用

    • 代码
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    func main() {
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(
    `{{range $x := . }}
        {{- println $x}}
    	{{- end}}`))
    	s := []string{"LiuBei","GuanYu","ZhangFei"}
    	t.Execute(os.Stdout, s)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 结果显示
    LiuBei
    GuanYu
    ZhangFei
    
    • 1
    • 2
    • 3

    5.2 $

    $是 顶级作用域对象,将整个作用域对象作为了一个变量

    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    func main() {
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(
        `{{- println $ }}`))
    	s := []string{"LiuBei","GuanYu","ZhangFei"}
    	t.Execute(os.Stdout, s)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 结果显示
    [LiuBei GuanYu ZhangFei]
    
    • 1

    如上可见,{{- println $ }} 将作用域对象打印了一遍。

    6. if语句

    • 语法
    {{if condition}} command {{end}}
    {{if condition-01}} command-01 {{else}} command-02 {{end}}
    {{if condition-01}} command-01 {{else if condition-02}} command-02 {{end}}
    
    • 1
    • 2
    • 3
    • 示例
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    func main() {
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(
        `{{- range  . }} 
    {{ println . }}
    {{- if   eq . "LiuBei" }}status: king {{- else}}status: minister {{end}}
    {{end}}`))
    	s := []string{"LiuBei","GuanYu","ZhangFei"}
    	t.Execute(os.Stdout, s)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 输出
    LiuBei
    status: king
     
    GuanYu
    status: minister 
     
    ZhangFei
    status: minister 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7. 迭代

    实际前边的示例我们已经使用过了

    7.1 迭代数组或切片

    • 完整代码
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    
    func main() {
    	nameList := []string{"GuanYu","ZhangFei","ZhaoYun"}
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(
        `{{ range  . }} {{- println . }} {{- end}}`))
    
    	_ = t.Execute(os.Stdout, nameList)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 输出
    GuanYu
    ZhangFei
    ZhaoYun
    
    • 1
    • 2
    • 3

    7.2 迭代 map

    7.2.1 仅处理值

    • 语法示例
    `{{ range $value := . }} {{- println $value }} {{- end}}`
    
    • 1
    • 完整示例
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    
    func main() {
    	nameList := map[string]string{"first":"GuanYu","second":"ZhangFei","third":"ZhaoYun"}
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(
        `{{ range $value := . }} {{- println $value }} {{- end}}`))
    
    	_ = t.Execute(os.Stdout, nameList)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 输出
    GuanYu
    ZhangFei
    ZhaoYun
    
    • 1
    • 2
    • 3

    7.2.2 处理 key和值

    • 语法示例
    `{{ range $key,$value := . }} {{- println $key "-" $value }} {{- end}}`
    
    • 1
    • 完整示例
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    
    func main() {
    	nameList := map[string]string{"first":"GuanYu","second":"ZhangFei","third":"ZhaoYun"}
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(
        `{{ range $key,$value := . }} {{- println $key "-" $value }} {{- end}}`))
    
    	_ = t.Execute(os.Stdout, nameList)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 结果显示
    first - GuanYu
    second - ZhangFei
    third - ZhaoYun
    
    • 1
    • 2
    • 3

    8. 内置函数

    8.1 比较

    内置函数说明
    eq等于{{if eq $x $y}}
    ne不等于{{if ne $x $y}}
    lt小于{{if lt $x $y}}
    le小于等于{{if le $x $y}}
    gt大于{{if gt $x $y}}
    ge大于等于{{if ge $x $y}}
    • 示例
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    
    func main() {
    	nameList := "a"
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(`
    {{- if ge 5 3 }} {{- println true}} {{- else }} {{- println false}} {{end}}
    {{- if ne 5 3 }} {{- println true}} {{- else }} {{- println false}} {{end}}
    {{- if eq "hello" "Hello" }} {{- println true}} {{- else }} {{- println false}} {{end}}
    `))
    
    	_ = t.Execute(os.Stdout, nameList)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    结果显示

    true
    true
    false
    
    • 1
    • 2
    • 3

    8.2 逻辑判断

    假: false,0
    真:true , !0

    内置函数说明示例
    and和(一个假则假,真返回最后一个真值){{and true false true }}
    or或(一个真则真,真返回第一个真值){{or 5 0 2}}
    not{{not true}}

    关于andor返回的真值很好理解:

    • and判断到最后一个值才能知道结果为真,因此真返回最后一个值。
    • or 判断到第一个真值则可断定结果为真,因此返回第一个真值。
    • 示例
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    
    func main() {
    	nameList := "a"
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(
        `{{- and 1 2 3 }} : and 全是真才真,返回最后一个真值
    {{ and 1 0 3 }} : and 一个假则假,返回假
    {{ or false false false }}  : or 全是假才假,返回假
    {{ or 1 0 3 }}  : or 一个真则真,返回第一个真值
    {{not 5}}  : not 真假取反
    {{not 0}}  : not 真假取反
    `))
    
    	_ = t.Execute(os.Stdout, nameList)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    结果显示

    3 : and 全是真才真,返回最后一个真值
    0 : and 一个假则假,返回假
    false  : or 全是假才假,返回假
    1  : or 一个真则真,返回第一个真值
    false  : not 真假取反
    true  : not 真假取反
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    8.3 其他

    内置函数说明示例
    print打印{{- print “liuBei”}}
    printf格式输出{{- printf “%d” $i}}
    println换行输出{{- println $i}}
    len字串/数组长度{{ $l := len “liuBei” }}
    index指向索引{{ $i := index 数组 索引 }}
    call调用函数{{ $s := call . 2 3 }}{{- printf “result : %d\n” $s}}
    • 示例 1
    package main
    
    import (
    	"os"
    	"text/template"
    )
    
    
    func main() {
    	nameList := []string{"liuBei","guanYu","zhangFei"}
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(`
    {{ $l := len . }}{{- printf "%d" $l}}
    {{ $i := index . 1 }}{{- println $i}}
    `))
    
    	_ = t.Execute(os.Stdout, nameList)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    输出

    3
    guanYu
    
    • 1
    • 2
    • 示例2(call)
    package main
    
    import (
    	"os"
    	"text/template"
    )
    func mySum(a int,b int)int{
    	sum := a + b
    	return sum
    }
    
    func main() {
    	t := template.New("xiShu")
    	t = template.Must(t.Parse(`
    {{ $s := call . 2 3 }}{{- printf "result : %d\n" $s}}
    `))
    	_ = t.Execute(os.Stdout,mySum )
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    结果

    result : 5
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    入门【网络安全/黑客】启蒙教程
    Java接入sqlserver的一些坑点记录
    面试题:经典常见排序算法 插入 冒泡 选择 归并 快速排序
    PHP 使用 Guzzle 执行 HTTP 请求
    (212)Verilog HDL:四位移位寄存器和递减计数器
    职场IT老鸟的几点小习惯
    VMware设置Linux网络
    postgres源码解析 SysLogger辅助进程
    CompletableFuture异步编排
    [数据结构] 树与二叉树
  • 原文地址:https://blog.csdn.net/xingzuo_1840/article/details/126596088