這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
在Go語言世界中,除了標準庫內建的html/template包之外,還有種類繁多的第三方模板引擎庫,這些庫大多來自其他語言的經驗繼承。
提起Go語言模板引擎,很多人自然會想到pongo2模板引擎,這也是筆者接觸最早的Go語言模板引擎,幾乎所有的Go主流流行的Web架構都對它提供支援,你甚至在xormplus/xorm這個資料庫架構庫中也能看到他的身影,它是一個像django文法的Go語言模板引擎,如果你以前做過python開發,那你一定會對它無比親切。
另一個筆者喜歡的Go語言模板引擎是jet,它功能強大,且高效,效能相當出色。另外一大特點是IDE支援,它有一個IDEA外掛程式可供開發人員使用,Github地址:https://github.com/jhsx/GoJetPlugin。這也是目前筆者主要使用的Go語言模板引擎之一(以前還有一些老項目是使用pongo2模板引擎的,另外xormplus/xorm中的sql模板也是採用pongo2)。
還有一個筆者想提及的Go語言模板引擎庫是go-template,它更像一個模板引擎介面卡,它的最大特色是同時支援standard html/template、amber、django、handlebars、pug(jade)、markdown六種模板引擎。
Go語言模板引擎其實主要分兩大類,一類是非先行編譯產生Go代碼的模板引擎(如以上提及的這些),另一類則是先行編譯產生Go代碼的模板引擎,這一類模板引擎由於先天優勢,效能將更為出色一些,是否採用這類引擎,要看你項目的需求來權衡。這類引擎的佼佼者是hero,也是一位國人開發的Go語言模板引擎庫。
目前筆者使用的Go語言模板引擎主要是pongo2,jet和hero。如果您還有想推薦的Go語言模板引擎庫,歡迎留言分享。
下面是我整理的Go語言模板引擎庫列表,相信總有一款適合您:)
Project Name |
Stars |
Forks |
Description |
pongo2 |
1028 |
103 |
Django-syntax like template-engine for Go |
mustache |
853 |
128 |
mustache.go is an implementation of the mustache template language in Go. |
hero |
795 |
39 |
A handy, fast and powerful go template engine. |
quicktemplate |
794 |
48 |
Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. |
amber |
746 |
44 |
Amber is an elegant templating engine for Go Programming Language, inspired from HAML and Jade |
ace |
616 |
32 |
Ace is an HTML template engine for Go. This is inspired by Slim and Jade. This is a refinement of Gold. |
gorazor |
594 |
70 |
GoRazor is the Go port of the razor view engine originated from asp.net in 2011. |
jet |
400 |
22 |
Jet is a template engine developed to be easy to use, powerful, dynamic, yet secure and very fast. |
ego |
323 |
25 |
Ego is an ERb style templating language for Go. It works by transpiling templates into pure Go and including them at compile time. These templates are light wrappers around the Go language itself. |
raymond |
186 |
16 |
Handlebars for golang |
fasttemplate |
143 |
24 |
Simple and fast template engine for Go |
soy |
120 |
18 |
Go implementation for Soy templates (Google Closure templates) |
kasia.go |
70 |
5 |
Kasia.go is a Go implementation of the Kasia templating system. |
ftmpl |
49 |
1 |
Fast typesafe templating for golang |
go-template |
30 |
3 |
The best way to work with different type of Template Engines and Parsers for Go Programming Language |
damsel |
19 |
1 |
Markup language featuring html outlining via css-selectors, extensible via pkg html/template and others. |
Ego |
15 |
0 |
Embedded Go – a Go-like template language |
下面是來自Go Template Benchmark的部分Go語言模板引擎庫的效能對比資料
full featured template engines
Name |
Runs |
µs/op |
B/op |
allocations/op |
Ace |
500,000 |
8.972 |
1,712 |
42 |
Amber |
1,000,000 |
5.628 |
1,440 |
38 |
Golang |
1,000,000 |
5.379 |
1,360 |
37 |
Handlebars |
500,000 |
10.174 |
4,210 |
82 |
JetHTML |
3,000,000 |
1.209 |
0 |
0 |
Kasia |
1,000,000 |
3.351 |
1,184 |
25 |
Mustache |
1,000,000 |
3.544 |
1,568 |
28 |
Pongo2 |
1,000,000 |
4.681 |
2,360 |
46 |
Soy |
1,000,000 |
3.067 |
1,376 |
25 |
precompilation to Go code
Name |
Runs |
µs/op |
B/op |
allocations/op |
Ego |
5,000,000 |
0.793 |
85 |
8 |
Egon |
3,000,000 |
1.541 |
149 |
12 |
EgonSlinso |
20,000,000 |
0.311 |
0 |
0 |
Ftmpl |
3,000,000 |
1.298 |
1,141 |
12 |
Gorazor |
5,000,000 |
1.014 |
613 |
11 |
Hero |
30,000,000 |
0.162 |
0 |
0 |
Quicktemplate |
20,000,000 |
0.289 |
0 |
0 |