text/template用法
1. 模板标签
模板标签用"{{"和"}}"括起来
2. 注释
# 使用"{{/*"和"*/}}"来包含注释内容
{{/* a comment */}}
3. 变量
# 输出当前变量值
{{.}}# 输出struct对象中字段或方法名称为"Abc"的值。
{{.Abc}}# 定义变量
# 变量名用字母和数字组成并带上"$"前缀,采用符号":="进行赋值。
{{$article := "hello"}}# 调用方法"Func",并传递参数,输出其返回值。
# 方法必须有一到两个返回值,如果是两个,第二个一定是error接口类型
{{.Func "参数1" "参数2"}}
4. 判断
{{if .condition1}} T1 {{else if .contition2}} T0 {{end}}# and 与
{{if and .condition1 .condition2}} T1 {{end}}# or 或
{{if or .condition1 .condition2}} T1 {{end}}# not 非
{{if not .condition}} T1 {{end}}# eq 等于
{{if eq .var1 .var2}} T1 {{end}}# ne 不等于
{{if ne .var1 .var2}} T1 {{end}}# lt 小于
{{if lt .var1 .var2}} T1 {{end}}# le 小于等于
{{if le .var1 .var2}} T1 {{end}}# gt 大于
{{if gt .var1 .var2}} T1 {{end}}# ge 大于等于
{{if ge .var1 .var2}} T1 {{end}}
5. 遍历
# 通过i,v来访问
{{range $i, $v := .slice}} {{$i}} => {{$v}} {{end}}# 通过.来访问
{{range .slice}} {{.}} {{end}}# 当没有可遍历值时,执行else
{{range .slice}} T1 {{else}} T0 {{end}}
6. 子模板嵌套
{{define "T1"}}ONE{{end}}
{{define "T2"}}TWO{{end}}
{{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
{{template "T3"}}
输出:
ONE TWO
7. 示例
7.1 字符串模板
type Inventory struct {Material stringCount uint
}func main() {sweaters := Inventory{"wool", 17}tmpl, err := template.New("test").Parse("{{.Count}} of {{.Material}}\n") // {{.Count}}获取struct的Count值if err != nil {panic(err)}err = tmpl.Execute(os.Stdout, sweaters) //返回 17 of woolif err != nil {panic(err)}
}
7.2 文件模板
type Inventory struct {Material stringCount uint
}/*
# cat templateContent.txt
{{.Count}} of {{.Material}}
*/
func main() {sweaters := Inventory{"wool", 17}tmpl, err := template.ParseFiles("templateContent.txt") //这里不需要使用new(),因为会默认使用文件名来命名if err != nil {panic(err)}err = tmpl.Execute(os.Stdout, sweaters) //返回 17 of woolif err != nil {panic(err)}
}
7.3 调用方法
type MyMethod struct {Name string
}func (my *MyMethod) SayHello() string { //没参数return "hello world"
}func (my *MyMethod) SayYouName(name string) string { //有参数return "my name is : " + name
}func main() {mine := &MyMethod{Name: "boss"}tmpl, err := template.New("test").Parse("{{.SayHello}}\n{{.SayYouName .Name}}\n")if err != nil {panic(err)}err = tmpl.Execute(os.Stdout, mine)if err != nil {panic(err)}// out:// hello world// my name is : boss
}
8. 文本和空格
空白符:空格,水平制表符,回车符和换行符。
默认情况下,在执行模板时,动作之间的所有文本都会逐字复制。
* 在左分隔符 “{{” 后面紧跟着一个减号和ASCII空格字符 “{{- “,则紧接在文本之前所有尾随空白符都将删除。
* 在右分隔符 “}}” 前面有空格和减号” -}}”,则紧随其后的文本所有前导空格都将删除。
"{{23 -}} < {{- 45}}"# 生成的输出将是
"23<45"
text/template用法相关推荐
- Text Template 模板
最近,使用到了 html/template 和 text/template 类库,通过使用模板,让项目清爽了不少.Go 的这个类似于和 PHP 的模板引擎,使用起来的话,类似于把大象关进冰箱的流程.第 ...
- T4 (Text Template Transformation Toolkit)实现简单实体代码生成
在很多场景下,我们都需要代码生成.你可以使用CodeSmith,不过它是商业软件.VisualStudio2008中自带也有代码生成功能.那就是T4 (Text Template Transforma ...
- <script type =“text / template”> ... </ script>的说明
本文翻译自:Explanation of I just stumbled upon something I've never seen before. 我偶然发现了一些我以前从未见过的东西. In t ...
- script type=text/template是干什么的,为什么要把html写在js中? 这是什么编程语言风格,都能这样用吗?...
这一段存放了一个模板. 在js里面,经常需要使用js往页面中插入html内容.比如这样: var number = 123; $('#d').append('<div class="t ...
- html/template 和 text/template区别
文章目录 前言 html/template text/template 前言 最近在学习go template,跟着一篇文章进行例程的学习,结果发现无论怎么调试,都没有办法复现例程的打印结果 纠结了一 ...
- text/template与html/template的区别
text/template 是将内容都已text文本格式返回. html/tempalte针对的是需要返回HTML内容的场景. 在模板渲染过程中会对一些有风险的内容进行转义,以此来防范跨站脚本攻击. ...
- Text Template Parser(多源数据提取软件)官方正式版V2.5 | 数据提取软件有哪些?
Text Template Parser 是一款简单实用的集数据检索.数据提取和数据转换的多数据源综合性文本数据提取软件,拥有友好的用户界面,可以帮助用户从包括文本文件.网页.电 ...
- html:text中readonly,HTML Input Text readOnly用法及代码示例
HTML DOM中的Input Text readOnly属性用于设置或返回文本字段是否应为只读.这意味着用户无法修改或更改特定元素中已经存在的内容(但是,用户可以对其进行制表,突出显示并从中复制文本 ...
- Go 源码阅读笔记 text/template/parse
为什么80%的码农都做不了架构师?>>> 文件组成 lex.go 词法定义与解析 node.go node 定义与创建 parse.go 生成 tem ...
最新文章
- 马少平、周枫、王小川、楼天城、唐文斌……清华计算机系与人工智能的40年...
- [转]ubuntu下ATI/Intel双显卡切换的方法
- CG CTF WEB 签到题
- 数据结构和算法之时间复杂度
- redis3.0.7_sds.c_sdsnewlen()
- NLP《语言模型(一)-- 基于统计的N-Gram语言模型》
- 调整KDevelop字体大小
- [Python] L1-014. 简单题-PAT团体程序设计天梯赛GPLT
- Telerik RadColorPicker 漂移问题的解决
- DataGrip连接不上本地localhost数据库解决办法
- hibernate4版本系列的官方下载地址
- Mac终端查看MD5/SHA1/SHA256
- uniapp H5 扫码 扫一扫 功能
- 运营必知的数据运营精髓
- 迪士尼机器人芭蕾舞_迪士尼“跳跳虎”机器人来了!
- 随便输入两个单词,两个单词之间以空格隔开,输出时每个单词的首字母变为大写。如输入:“hello java”,输出为“Hello Java”
- ensp两个路由的配置(想对全世界说晚安 恰好你就是全世界)
- 虚拟服务器 补丁,两种方法为VMware ESXi打补丁
- 网络营销实战课-好用的工具推荐
- XAMPP/PHPnow/phpStudy安装使用对比