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
$是 顶级作用域对象,将整个作用域对象作为了一个变量
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 $ }}将作用域对象打印了一遍。
{{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
实际前边的示例我们已经使用过了
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
`{{ 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
`{{ 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
| 内置函数 | 说明 | |
|---|---|---|
| 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
假:
false,0
真:true,!0
| 内置函数 | 说明 | 示例 |
|---|---|---|
| and | 和(一个假则假,真返回最后一个真值) | {{and true false true }} |
| or | 或(一个真则真,真返回第一个真值) | {{or 5 0 2}} |
| not | 非 | {{not true}} |
关于
and和or返回的真值很好理解:
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 真假取反
| 内置函数 | 说明 | 示例 |
|---|---|---|
| 打印 | {{- 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}} |
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
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
