Go 每日一庫(kù)之 quicktemplate
簡(jiǎn)介
最近在整理我們項(xiàng)目代碼的時(shí)候,發(fā)現(xiàn)有很多活動(dòng)的代碼在結(jié)構(gòu)和提供的功能上都非常相似。為了方便今后的開(kāi)發(fā),我花了一點(diǎn)時(shí)間編寫(xiě)了一個(gè)生成代碼框架的工具,最大程度地降低重復(fù)勞動(dòng)。代碼本身并不復(fù)雜,且與項(xiàng)目代碼關(guān)聯(lián)性較大,這里就不展開(kāi)介紹了。在這個(gè)過(guò)程中,我發(fā)現(xiàn) Go 標(biāo)準(zhǔn)的模板庫(kù)text/template和html/template使用起來(lái)比較束手束腳,很不方便。我從 GitHub 了解到quicktemplate這個(gè)第三方模板庫(kù),功能強(qiáng)大,語(yǔ)法簡(jiǎn)單,使用方便。今天我們就來(lái)介紹一下quicktemplate。
快速使用
本文代碼使用 Go Modules。
先創(chuàng)建代碼目錄并初始化:
$ mkdir quicktemplate && cd quicktemplate
$ go mod init github.com/darjun/go-daily-lib/quicktemplate
quicktemplate會(huì)將我們編寫(xiě)的模板代碼轉(zhuǎn)換為 Go 語(yǔ)言代碼。因此我們需要安裝quicktemplate包和一個(gè)名為qtc的編譯器:
$ go get -u github.com/valyala/quicktemplate
$ go get -u github.com/valyala/quicktemplate/qtc
首先,我們需要編寫(xiě)quicktemplate格式的模板文件,模板文件默認(rèn)以.qtpl作為擴(kuò)展名。下面我編寫(xiě)了一個(gè)簡(jiǎn)單的模板文件greeting.qtpl:
All text outside function is treated as comments.
{% func Greeting(name string, count int) %}
{% for i := 0; i < count; i++ %}
Hello, {%s name %}
{% endfor %}
{% endfunc %}
模板語(yǔ)法非常簡(jiǎn)單,我們只需要簡(jiǎn)單了解以下 2 點(diǎn):
模板以函數(shù)為單位,函數(shù)可以接受任意類型和數(shù)量的參數(shù),這些參數(shù)可以在函數(shù)中使用。所有函數(shù)外的文本都是注釋, qtc編譯時(shí)會(huì)忽視注釋;函數(shù)內(nèi)的內(nèi)容,除了語(yǔ)法結(jié)構(gòu),其他都會(huì)原樣輸出到渲染后的文本中,包括空格和換行。
將greeting.qtpl保存到templates目錄,然后執(zhí)行qtc命令。該命令會(huì)生成對(duì)應(yīng)的 Go 文件greeting.qtpl.go,包名為templates。現(xiàn)在,我們就可以使用這個(gè)模板了:
package?main
import?(
??"fmt"
??"github.com/darjun/go-daily-lib/quicktemplate/get-started/templates"
)
func?main()?{
??fmt.Println(templates.Greeting("dj",?5))
}
調(diào)用模板函數(shù),傳入?yún)?shù),返回渲染后的文本:
$ go run .
Hello, dj
Hello, dj
Hello, dj
Hello, dj
Hello, dj
{%s name %}執(zhí)行文本替換,{% for %}循環(huán)生成重復(fù)文本。輸出中出現(xiàn)多個(gè)空格和換行,這是因?yàn)?strong>函數(shù)內(nèi)除了語(yǔ)法結(jié)構(gòu),其他內(nèi)容都會(huì)原樣保留,包括空格和換行。
需要注意的是,由于quicktemplate是將模板轉(zhuǎn)換為 Go 代碼使用的,所以如果模板有修改,必須先執(zhí)行qtc命令重新生成 Go 代碼,否則修改不生效。
語(yǔ)法結(jié)構(gòu)
quicktemplate支持 Go 常見(jiàn)的語(yǔ)法結(jié)構(gòu),if/for/func/import/return。而且寫(xiě)法與直接寫(xiě) Go 代碼沒(méi)太大的區(qū)別,幾乎沒(méi)有學(xué)習(xí)成本。只是在模板中使用這些語(yǔ)法時(shí),需要使用{%和%}包裹起來(lái),而且if和for等需要添加endif/endfor明確表示結(jié)束。
變量
上面我們已經(jīng)看到如何渲染傳入的參數(shù)name,使用{%s name %}。由于name是 string 類型,所以在{%后使用s指定類型。quicktemplate還支持其他類型的值:
整型: {%d int %},{%dl int64 %},{%dul uint64 %};浮點(diǎn)數(shù): {%f float %}。還可以設(shè)置輸出的精度,使用{%f.precision float %}。例如{%f.2 1.2345 %}輸出1.23;字節(jié)切片( []byte):{%z bytes %};字符串: {%q str %}或字節(jié)切片:{%qz bytes %},引號(hào)轉(zhuǎn)義為";字符串: {%j str %}或字節(jié)切片:{%jz bytes %},沒(méi)有引號(hào);URL 編碼: {%u str %},{%uz bytes %};{%v anything %}:輸出等同于fmt.Sprintf("%v", anything)。
先編寫(xiě)模板:
{% func Types(a int, b float64, c []byte, d string) %}
int: {%d a %}, float64: {%f.2 b %}, bytes: {%z c %}, string with quotes: {%q d %}, string without quotes: {%j d %}.
{% endfunc %}
然后使用:
func?main()?{
??fmt.Println(templates.Types(1,?5.75,?[]byte{'a',?'b',?'c'},?"hello"))
}
運(yùn)行:
$ go run .
int: 1, float64: 5.75, bytes: abc, string with quotes: "hello", string without quotes: hello.
調(diào)用函數(shù)
quicktemplate支持在模板中調(diào)用模板函數(shù)、標(biāo)準(zhǔn)庫(kù)的函數(shù)。由于qtc會(huì)直接生成 Go 代碼,我們甚至還可以在同目錄下編寫(xiě)自己的函數(shù)給模板調(diào)用,模板 A 中也可以調(diào)用模板 B 中定義的函數(shù)。
我們先在templates目錄下編寫(xiě)一個(gè)文件rank.go,定義一個(gè)Rank函數(shù),傳入分?jǐn)?shù),返回評(píng)級(jí):
package?templates
func?Rank(score?int)?string?{
??if?score?>=?90?{
????return?"A"
??}?else?if?score?>=?80?{
????return?"B"
??}?else?if?score?>=?70?{
????return?"C"
??}?else?if?score?>=?60?{
????return?"D"
??}?else?{
????return?"E"
??}
}
然后我們可以在模板中調(diào)用這個(gè)函數(shù):
{% import "fmt" %}
{% func ScoreList(name2score map[string]int) %}
{% for name, score := range name2score %}
{%s fmt.Sprintf("%s: score-%d rank-%s", name, score, Rank(score)) %}
{% endfor %}
{% endfunc %}
編譯模板:
$ qtc
編寫(xiě)程序:
func?main()?{
??name2score?:=?make(map[string]int)
??name2score["dj"]?=?85
??name2score["lizi"]?=?96
??name2score["hjw"]?=?52
??fmt.Println(templates.ScoreList(name2score))
}
運(yùn)行程序輸出:
$ go run .
dj: score-85 rank-B
lizi: score-96 rank-A
hjw: score-52 rank-E
由于我們?cè)谀0逯杏玫?code style="font-size: 14px;overflow-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(239, 112, 96);">fmt包,需要先使用{% import %}將該包導(dǎo)入。
在模板中調(diào)用另一個(gè)模板的函數(shù)也是類似的,因?yàn)槟0遄罱K都會(huì)轉(zhuǎn)為 Go 代碼。Go 代碼中有同樣簽名的函數(shù)。
Web
quicktemplate常用來(lái)編寫(xiě) HTML 頁(yè)面的模板:
{% func Index(name string) %}
Awesome Web
Hi, {%s name %}
Welcome to the awesome web!!!
{% endfunc %}
下面編寫(xiě)一個(gè)簡(jiǎn)單的 Web 服務(wù)器:
func?index(w?http.ResponseWriter,?r?*http.Request)?{
??templates.WriteIndex(w,?r.FormValue("name"))
}
func?main()?{
??mux?:=?http.NewServeMux()
??mux.HandleFunc("/",?index)
??server?:=?&http.Server{
????Handler:?mux,
????Addr:????":8080",
??}
??log.Fatal(server.ListenAndServe())
}
qtc會(huì)生成一個(gè)Write*的方法,它接受一個(gè)io.Writer的參數(shù)。將模板渲染的結(jié)果寫(xiě)入這個(gè)io.Writer中,我們可以直接將http.ResponseWriter作為參數(shù)傳入,非常便捷。
運(yùn)行:
$ qtc
$ go run .
瀏覽器輸入localhost:8080?name=dj查看結(jié)果。
總結(jié)
quicktemplate至少有下面 3 個(gè)優(yōu)勢(shì):
語(yǔ)法與 Go 語(yǔ)言非常類似,幾乎沒(méi)有學(xué)習(xí)成本; 會(huì)先轉(zhuǎn)換為 Go,渲染速度非常快,比標(biāo)準(zhǔn)庫(kù) html/template快 20 倍以上;為了安全考慮,會(huì)執(zhí)行一些編碼,避免受到攻擊。
從我個(gè)人的實(shí)際使用情況來(lái)看,確實(shí)很方便,很實(shí)用。感興趣的還可以去看看qtc生成的 Go 代碼。
大家如果發(fā)現(xiàn)好玩、好用的 Go 語(yǔ)言庫(kù),歡迎到 Go 每日一庫(kù) GitHub 上提交 issue?
參考
quicktemplate GitHub:https://github.com/valyala/quicktemplate Go 每日一庫(kù) GitHub:https://github.com/darjun/go-daily-lib
推薦閱讀

