当前位置: 首页 > news >正文

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

文章目录

  • 5. 变量
    • 5.1 变量使用
    • 5.2 $
  • 6. if语句
  • 7. 迭代
    • 7.1 迭代数组或切片
    • 7.2 迭代 map
      • 7.2.1 仅处理值
      • 7.2.2 处理 key和值
  • 8. 内置函数
    • 8.1 比较
    • 8.2 逻辑判断
    • 8.3 其他

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)
}
  • 结果显示
LiuBei
GuanYu
ZhangFei

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)
}
  • 结果显示
[LiuBei GuanYu ZhangFei]

如上可见,{{- 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}}
  • 示例
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)
}
  • 输出
LiuBei
status: king
 
GuanYu
status: minister 
 
ZhangFei
status: minister 

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)
}
  • 输出
GuanYu
ZhangFei
ZhaoYun

7.2 迭代 map

7.2.1 仅处理值

  • 语法示例
`{{ range $value := . }} {{- println $value }} {{- end}}`
  • 完整示例
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)
}
  • 输出
GuanYu
ZhangFei
ZhaoYun

7.2.2 处理 key和值

  • 语法示例
`{{ range $key,$value := . }} {{- println $key "-" $value }} {{- end}}`
  • 完整示例
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)
}
  • 结果显示
first - GuanYu
second - ZhangFei
third - ZhaoYun

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)
}

结果显示

true
true
false

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)
}

结果显示

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

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)
}

输出

3
guanYu
  • 示例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 )
}

结果

result : 5

在这里插入图片描述

相关文章:

  • JavaScript(三):理解异步
  • JVM阶段(3)-OutOfMemoryError异常
  • 企业运维容器之 docker 网络
  • 【QML】 如何导入QML文档目录
  • 【前端】命令行基础,linux常用命令
  • 【ZYNQ-嵌入式】zynq学习笔记(二)—— GIPO的硬件配置和软件配置
  • vue echarts 镂空饼图配置
  • 项目二:《贪吃蛇》
  • 企业运维容器之 docker仓库
  • 快速排序sort 第k个数
  • uniapp开发微信小程序Error in onLoad hook: “SyntaxError: Unexpected end of JSON input“
  • MySQL当前链接状态查询
  • 打破平台限制,小程序如何在硬件设备上运行?
  • ORA-01017(:用户名/口令无效; 登录被拒绝)Oracle新建用户并授权
  • PostgreSQL的学习心得和知识总结(九十九)|语法级自上而下完美实现达梦数据库的 TOP语法功能 的实现方案
  • Android Studio:GIT提交项目到远程仓库
  • android 一些 utils
  • git 常用命令
  • Meteor的表单提交:Form
  • October CMS - 快速入门 9 Images And Galleries
  • orm2 中文文档 3.1 模型属性
  • spring + angular 实现导出excel
  • Tornado学习笔记(1)
  • 给第三方使用接口的 URL 签名实现
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • #{}和${}的区别?
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • %@ page import=%的用法
  • (0)Nginx 功能特性
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (floyd+补集) poj 3275
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (附源码)计算机毕业设计ssm电影分享网站
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (转)自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版
  • .Mobi域名介绍
  • .netcore 获取appsettings
  • .NET处理HTTP请求
  • .Net下的签名与混淆
  • /proc/vmstat 详解
  • @ 代码随想录算法训练营第8周(C语言)|Day53(动态规划)
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...
  • [2013AAA]On a fractional nonlinear hyperbolic equation arising from relative theory
  • [20171106]配置客户端连接注意.txt
  • [Android 数据通信] android cmwap接入点
  • [C#小技巧]如何捕捉上升沿和下降沿
  • [CISCN 2019华东南]Web11
  • [Grafana]ES数据源Alert告警发送
  • [HUBUCTF 2022 新生赛]
  • [IM] [Webhook] Webhook实现IM平台机器人