在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 |
有疑问加站长微信联系(非本文作者)