Go template詳解(中)- 變量使用另患、if語句、迭代(數(shù)組蛾绎、切片昆箕、map)、內(nèi)置函數(shù)(比較租冠、邏輯判斷鹏倘、打印、索引顽爹、函數(shù)調(diào)用)

@[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 xy}}
ne 不等于 {{if ne xy}}
lt 小于 {{if lt xy}}
le 小于等于 {{if le xy}}
gt 大于 {{if gt xy}}
ge 大于等于 {{if ge xy}}
  • 示例
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)于andor返回的真值很好理解:

  • 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 打印 {{- print "liuBei"}}
printf 格式輸出 {{- printf "%d" $i}}
println 換行輸出 {{- println $i}}
len 字串/數(shù)組長度 {{ $l := len "liuBei" }}
index 指向索引 {{ $i := index 數(shù)組 索引 }}
call 調(diào)用函數(shù) {{ 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 )
}

結(jié)果

result : 5

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市盗扒,隨后出現(xiàn)的幾起案子跪楞,更是在濱河造成了極大的恐慌,老刑警劉巖侣灶,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甸祭,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡褥影,警方通過查閱死者的電腦和手機(jī)池户,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來凡怎,“玉大人校焦,你說我怎么就攤上這事≌ぬ” “怎么了斟湃?”我有些...
    開封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長檐薯。 經(jīng)常有香客問我凝赛,道長,這世上最難降的妖魔是什么坛缕? 我笑而不...
    開封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任墓猎,我火速辦了婚禮,結(jié)果婚禮上赚楚,老公的妹妹穿的比我還像新娘毙沾。我一直安慰自己,他們只是感情好宠页,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開白布左胞。 她就那樣靜靜地躺著寇仓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪烤宙。 梳的紋絲不亂的頭發(fā)上遍烦,一...
    開封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音躺枕,去河邊找鬼服猪。 笑死,一個(gè)胖子當(dāng)著我的面吹牛拐云,可吹牛的內(nèi)容都是我干的罢猪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼叉瘩,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼膳帕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起房揭,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤备闲,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后捅暴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體恬砂,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年蓬痒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了泻骤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡梧奢,死狀恐怖狱掂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情亲轨,我是刑警寧澤趋惨,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站惦蚊,受9級(jí)特大地震影響器虾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蹦锋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一兆沙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧莉掂,春花似錦葛圃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽曲楚。三九已至,卻和暖如春褥符,著一層夾襖步出監(jiān)牢的瞬間洞渤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來泰國打工属瓣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人讯柔。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓抡蛙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親魂迄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子粗截,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容