@[toc]
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)
}
- 結(jié)果顯示
LiuBei
GuanYu
ZhangFei
5.2 $
$
是 頂級(jí)作用域?qū)ο笙吮茫瑢⒄麄€(gè)作用域?qū)ο笞鳛榱艘粋€(gè)變量
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)
}
- 結(jié)果顯示
[LiuBei GuanYu ZhangFei]
如上可見,
{{- println $ }}
將作用域?qū)ο蟠蛴×艘槐椤?/p>
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. 迭代
實(shí)際前邊的示例我們已經(jīng)使用過了
7.1 迭代數(shù)組或切片
- 完整代碼
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)
}
- 結(jié)果顯示
first - GuanYu
second - ZhangFei
third - ZhaoYun
8. 內(nèi)置函數(shù)
8.1 比較
內(nèi)置函數(shù) | 說明 | |
---|---|---|
eq | 等于 | {{if eq |
ne | 不等于 | {{if ne |
lt | 小于 | {{if lt |
le | 小于等于 | {{if le |
gt | 大于 | {{if gt |
ge | 大于等于 | {{if ge |
- 示例
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)
}
結(jié)果顯示
true
true
false
8.2 邏輯判斷
假:
false
,0
真:true
,!0
內(nèi)置函數(shù) | 說明 | 示例 |
---|---|---|
and | 和(一個(gè)假則假话原,真返回最后一個(gè)真值) | {{and true false true }} |
or | 或(一個(gè)真則真夕吻,真返回第一個(gè)真值) | {{or 5 0 2}} |
not | 非 | {{not true}} |
關(guān)于
and
和or
返回的真值很好理解:
and
判斷到最后一個(gè)值才能知道結(jié)果為真,因此真返回最后一個(gè)值繁仁。or
判斷到第一個(gè)真值則可斷定結(jié)果為真涉馅,因此返回第一個(gè)真值。
- 示例
package main
import (
"os"
"text/template"
)
func main() {
nameList := "a"
t := template.New("xiShu")
t = template.Must(t.Parse(
`{{- and 1 2 3 }} : and 全是真才真黄虱,返回最后一個(gè)真值
{{ and 1 0 3 }} : and 一個(gè)假則假稚矿,返回假
{{ or false false false }} : or 全是假才假,返回假
{{ or 1 0 3 }} : or 一個(gè)真則真捻浦,返回第一個(gè)真值
{{not 5}} : not 真假取反
{{not 0}} : not 真假取反
`))
_ = t.Execute(os.Stdout, nameList)
}
結(jié)果顯示
3 : and 全是真才真晤揣,返回最后一個(gè)真值
0 : and 一個(gè)假則假,返回假
false : or 全是假才假朱灿,返回假
1 : or 一個(gè)真則真昧识,返回第一個(gè)真值
false : not 真假取反
true : not 真假取反
8.3 其他
內(nèi)置函數(shù) | 說明 | 示例 |
---|---|---|
打印 | {{- print "liuBei"}} | |
printf | 格式輸出 | {{- printf "%d" $i}} |
println | 換行輸出 | {{- println $i}} |
len | 字串/數(shù)組長度 | {{ $l := len "liuBei" }} |
index | 指向索引 | {{ $i := index 數(shù)組 索引 }} |
call | 調(diào)用函數(shù) | {{ |
- 示例 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 )
}
結(jié)果
result : 5