最近在整理我們項目代碼的時候,發(fā)現(xiàn)有很多活動的代碼在結(jié)構(gòu)和提供的功能上都非常相似。為了方便今后的開發(fā),我花了一點時間編寫了一個生成代碼框架的工具,最大程度地降低重復勞動。代碼本身并不復雜,且與項目代碼關(guān)聯(lián)性較大,這里就不展開介紹了。在這個過程中,我發(fā)現(xiàn) Go 標準的模板庫text/template和html/template使用起來比較束手束腳,很不方便。我從 GitHub 了解到quicktemplate這個第三方模板庫,功能強大,語法簡單,使用方便。今天我們就來介紹一下quicktemplate。
本文代碼使用 Go Modules。
先創(chuàng)建代碼目錄并初始化:
$ mkdir quicktemplate cd quicktemplate $ go mod init github.com/darjun/go-daily-lib/quicktemplate
quicktemplate會將我們編寫的模板代碼轉(zhuǎn)換為 Go 語言代碼。因此我們需要安裝quicktemplate包和一個名為qtc的編譯器:
$ go get -u github.com/valyala/quicktemplate $ go get -u github.com/valyala/quicktemplate/qtc
首先,我們需要編寫quicktemplate格式的模板文件,模板文件默認以.qtpl作為擴展名。下面我編寫了一個簡單的模板文件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 %}
模板語法非常簡單,我們只需要簡單了解以下 2 點:
將greeting.qtpl保存到templates目錄,然后執(zhí)行qtc命令。該命令會生成對應的 Go 文件greeting.qtpl.go,包名為templates。現(xiàn)在,我們就可以使用這個模板了:
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, djHello, dj
Hello, dj
Hello, dj
Hello, dj
{%s name %}執(zhí)行文本替換,{% for %}循環(huán)生成重復文本。輸出中出現(xiàn)多個空格和換行,這是因為函數(shù)內(nèi)除了語法結(jié)構(gòu),其他內(nèi)容都會原樣保留,包括空格和換行。
需要注意的是,由于quicktemplate是將模板轉(zhuǎn)換為 Go 代碼使用的,所以如果模板有修改,必須先執(zhí)行qtc命令重新生成 Go 代碼,否則修改不生效。
quicktemplate支持 Go 常見的語法結(jié)構(gòu),if/for/func/import/return。而且寫法與直接寫 Go 代碼沒太大的區(qū)別,幾乎沒有學習成本。只是在模板中使用這些語法時,需要使用{%和%}包裹起來,而且if和for等需要添加endif/endfor明確表示結(jié)束。
上面我們已經(jīng)看到如何渲染傳入的參數(shù)name,使用{%s name %}。由于name是 string 類型,所以在{%后使用s指定類型。quicktemplate還支持其他類型的值:
先編寫模板:
{% 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")) }
運行:
$ go run .
int: 1, float64: 5.75, bytes: abc, string with quotes: quot;helloquot;, string without quotes: hello.
quicktemplate支持在模板中調(diào)用模板函數(shù)、標準庫的函數(shù)。由于qtc會直接生成 Go 代碼,我們甚至還可以在同目錄下編寫自己的函數(shù)給模板調(diào)用,模板 A 中也可以調(diào)用模板 B 中定義的函數(shù)。
我們先在templates目錄下編寫一個文件rank.go,定義一個Rank函數(shù),傳入分數(shù),返回評級:
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)用這個函數(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
編寫程序:
func main() { name2score := make(map[string]int) name2score["dj"] = 85 name2score["lizi"] = 96 name2score["hjw"] = 52 fmt.Println(templates.ScoreList(name2score)) }
運行程序輸出:
$ go run .
dj: score-85 rank-B
lizi: score-96 rank-A
hjw: score-52 rank-E
由于我們在模板中用到fmt包,需要先使用{% import %}將該包導入。
在模板中調(diào)用另一個模板的函數(shù)也是類似的,因為模板最終都會轉(zhuǎn)為 Go 代碼。Go 代碼中有同樣簽名的函數(shù)。
quicktemplate常用來編寫 HTML 頁面的模板:
{% func Index(name string) %} html> head> title>Awesome Web/title> /head> body> h1>Hi, {%s name %} p>Welcome to the awesome web!!!/p> /body> /html> {% endfunc %}
下面編寫一個簡單的 Web 服務器:
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會生成一個Write*的方法,它接受一個io.Writer的參數(shù)。將模板渲染的結(jié)果寫入這個io.Writer中,我們可以直接將http.ResponseWriter作為參數(shù)傳入,非常便捷。
運行:
$ qtc
$ go run .
瀏覽器輸入localhost:8080?name=dj查看結(jié)果。
quicktemplate至少有下面 3 個優(yōu)勢:
從我個人的實際使用情況來看,確實很方便,很實用。感興趣的還可以去看看qtc生成的 Go 代碼。
大家如果發(fā)現(xiàn)好玩、好用的 Go 語言庫,歡迎到 Go 每日一庫 GitHub 上提交 issue😄
quicktemplate GitHub:https://github.com/valyala/quicktemplate
Go 每日一庫 GitHub:https://github.com/darjun/go-daily-lib
到此這篇關(guān)于Go每日一庫之quicktemplate的使用的文章就介紹到這了,更多相關(guān)Go quicktemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標簽:江西 嘉峪關(guān) 宜賓 黑龍江 武漢 新余 延邊 張掖
巨人網(wǎng)絡通訊聲明:本文標題《Go每日一庫之quicktemplate的使用》,本文關(guān)鍵詞 每日,一庫,之,quicktemplate,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。