在Go語言世界中,除了標準庫自帶的html/template包以外,還有種類繁多的第三方模板引擎庫,這些庫大多來自其餘語言的經驗繼承。css
提起Go語言模板引擎,不少人天然會想到pongo2模板引擎,這也是筆者接觸最先的Go語言模板引擎,幾乎全部的Go主流流行的Web框架都對它提供支持,你甚至在xormplus/xorm這個數據庫框架庫中也能看到他的身影,它是一個像django語法的Go語言模板引擎,若是你之前作過python開發,那你必定會對它無比親切。html
另外一個筆者喜歡的Go語言模板引擎是jet,它功能強大,且高效,性能至關出色。另一大特色是IDE支持,它有一個IDEA插件可供開發者使用,Github地址:https://github.com/jhsx/GoJetPlugin。這也是目前筆者主要使用的Go語言模板引擎之一(之前還有一些老項目是使用pongo2模板引擎的,另外xormplus/xorm中的sql模板也是採用pongo2)。python
還有一個筆者想說起的Go語言模板引擎庫是go-template,它更像一個模板引擎適配器,它的最大特點是同時支持standard html/template、amber、django、handlebars、pug(jade)、markdown六種模板引擎。git
Go語言模板引擎其實主要分兩大類,一類是非預編譯生成Go代碼的模板引擎(如以上說起的這些),另外一類則是預編譯生成Go代碼的模板引擎,這一類模板引擎因爲先天優點,性能將更爲出色一些,是否採用這類引擎,要看你項目的需求來權衡。這類引擎的佼佼者是hero,也是一位國人開發的Go語言模板引擎庫。github
目前筆者使用的Go語言模板引擎主要是pongo2,jet和hero。若是您還有想推薦的Go語言模板引擎庫,歡迎留言分享。golang
下面是我整理的Go語言模板引擎庫列表,相信總有一款適合您:)sql
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語言模板引擎庫的性能對比數據數據庫
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 |
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 |