在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
1028 103 Django-syntax like template-engine for Go
853 128 mustache.go is an implementation of the mustache template language in Go.
795 39 A handy, fast and powerful go template engine.
794 48 Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths.
746 44 Amber is an elegant templating engine for Go Programming Language, inspired from HAML and Jade
616 32 Ace is an HTML template engine for Go. This is inspired by Slim and Jade. This is a refinement of Gold.
594 70 GoRazor is the Go port of the razor view engine originated from asp.net in 2011.
400 22 Jet is a template engine developed to be easy to use, powerful, dynamic, yet secure and very fast.
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.
186 16 Handlebars for golang
143 24 Simple and fast template engine for Go
120 18 Go implementation for Soy templates (Google Closure templates)
70 5 Kasia.go is a Go implementation of the Kasia templating system.
49 1 Fast typesafe templating for golang
30 3 The best way to work with different type of Template Engines and Parsers for Go Programming Language
19 1 Markup language featuring html outlining via css-selectors, extensible via pkg html/template and others.
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