https://github.com/OblivionOcean/Goh
Goh 是一款Go語言的預編譯快速模板引擎。
English | 簡體中文
目錄
- 特性
- 性能測試
- 安裝
- 使用
- 語法
特性
- 預編譯模板引擎,提升運行速度。
- 幾乎兼容·Go語言的語法。
- 0依賴。
- 更改模板文件後自動重新編譯。
性能測試
從 https://github.com/slinso/goTemplateBenchmark 獲取,目前為本地測試結果,代碼與Hero部分的測試代碼相同,BenchmarkComplexGoDirectBuffer和BenchmarkComplexGoStaticString分別是寫入Buffer和靜態String,所以不做計算,因此複雜模板測試排名第一
goos: windows
goarch: amd64
pkg: github.com/SlinSo/goTemplateBenchmark
cpu: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
# 複雜模板測試
BenchmarkComplexGolang-16 36800 31428 ns/op 6562 B/op 290 allocs/op
BenchmarkComplexGolangText-16 88148 13370 ns/op 2235 B/op 107 allocs/op
BenchmarkComplexEgo-16 486294 2411 ns/op 568 B/op 31 allocs/op
BenchmarkComplexQuicktemplate-16 1367928 878.1 ns/op 0 B/op 0 allocs/op
BenchmarkComplexTempl-16 788673 1400 ns/op 408 B/op 11 allocs/op
BenchmarkComplexFtmpl-16 293755 3982 ns/op 3534 B/op 38 allocs/op
BenchmarkComplexFtmplInclude-16 317361 4142 ns/op 3534 B/op 38 allocs/op
BenchmarkComplexMustache-16 90567 13748 ns/op 7274 B/op 156 allocs/op
BenchmarkComplexGorazor-16 361304 3195 ns/op 3688 B/op 24 allocs/op
BenchmarkComplexJetHTML-16 189176 5928 ns/op 532 B/op 5 allocs/op
BenchmarkComplexHero-16 1410391 863.5 ns/op 0 B/op 0 allocs/op
BenchmarkComplexGoh-16 2304783 535.4 ns/op 0 B/op 0 allocs/op
BenchmarkComplexJade-16 1826784 651.8 ns/op 0 B/op 0 allocs/op
BenchmarkComplexGoDirectBuffer-16 2890996 414.6 ns/op 0 B/op 0 allocs/op
BenchmarkComplexGoHyperscript-16 1717754 778.6 ns/op 0 B/op 0 allocs/op
BenchmarkComplexGoStaticString-16 84003024 14.44 ns/op 0 B/op 0 allocs/op
# 簡單模板測試
BenchmarkGolang-16 300493 3691 ns/op 768 B/op 35 allocs/op
BenchmarkGolangText-16 1000000 1073 ns/op 128 B/op 7 allocs/op
BenchmarkGoDirectBuffer-16 21959280 55.81 ns/op 0 B/op 0 allocs/op
BenchmarkGoCustomHtmlAPI-16 14034298 85.06 ns/op 0 B/op 0 allocs/op
BenchmarkGoFunc3-16 14962965 68.62 ns/op 0 B/op 0 allocs/op
BenchmarkEgo-16 2577276 464.3 ns/op 85 B/op 8 allocs/op
BenchmarkHB-16 280617 4445 ns/op 2448 B/op 51 allocs/op
BenchmarkQuicktemplate-16 7013572 168.9 ns/op 0 B/op 0 allocs/op
BenchmarkFtmpl-16 1000000 1000 ns/op 774 B/op 12 allocs/op
BenchmarkAce-16 179811 6605 ns/op 1121 B/op 40 allocs/op
BenchmarkAmber-16 268149 3800 ns/op 849 B/op 36 allocs/op
BenchmarkMustache-16 523143 2636 ns/op 1722 B/op 30 allocs/op
BenchmarkPongo2-16 350612 3862 ns/op 2074 B/op 32 allocs/op
BenchmarkHandlebars-16 162860 7261 ns/op 3423 B/op 75 allocs/op
BenchmarkGorazor-16 1562088 772.3 ns/op 512 B/op 5 allocs/op
BenchmarkSoy-16 639549 2200 ns/op 1224 B/op 19 allocs/op
BenchmarkJetHTML-16 1960117 600.4 ns/op 0 B/op 0 allocs/op
BenchmarkHero-16 10452396 113.9 ns/op 0 B/op 0 allocs/op
BenchmarkGoh-16 14838537 81.97 ns/op 0 B/op 0 allocs/op
BenchmarkJade-16 15025261 78.85 ns/op 0 B/op 0 allocs/op
BenchmarkTempl-16 4015622 293.1 ns/op 96 B/op 2 allocs/op
BenchmarkGomponents-16 479330 2882 ns/op 1112 B/op 56 allocs/op
ok github.com/SlinSo/goTemplateBenchmark 65.553s
安裝
go get -u github.com/OblivionOcean/Goh
go install github.com/OblivionOcean/Goh
# 依賴
go get golang.org/x/tools/cmd/goimports
go install golang.org/x/tools/cmd/goimports
使用
~ $ Goh
Usage of ./Goh:
-dest string
generated golang files dir, it will be the same with source if not set
-ext string
source file extensions, comma splitted if many (default ".html")
-pkg string
the generated template package name, default is template (default "template")
-src string
the html template file or directory (default "./")
完整的使用方法請參考實例程序
<%: func UserList(title string, userList []string, buf *bytes.Buffer) %>
<!DOCTYPE html>
<html>
<head>
<title>
<%= title %>
</title>
</head>
<body>
<h1>
<%= title %>
</h1>
<ul>
<% for _, user :=range userList { %>
<% if user !="Alice" { %>
<li>
<%= user %>
</li>
<% } %>
<% } %>
</ul>
</body>
</html>
package main
import (
"bytes"
"net/http"
"github.com/OblivionOcean/Goh/example/template"
)
func main() {
http.HandleFunc("/users", func(w http.ResponseWriter, req *http.Request) {
var userList = []string{
"Alice",
"Bob",
"Tom",
}
buffer := new(bytes.Buffer)
template.UserList("User List", userList, buffer)
w.Write(buffer.Bytes())
})
http.ListenAndServe(":8080", nil)
}
語法
文檔修改自https://github.com/shiyanhui/hero
Goh總共有九種語句,他們分別是:
-
函數定義語句
<%: func define %>- 該語句定義了該模板所對應的函數,如果一個模板中沒有函數定義語句,那麼最終結果不會生成對應的函數。
- 該函數最後一個參數必須為
*bytes.Buffer或者io.Writer, hero會自動識別該參數的名字,並把把結果寫到該參數裏。 -
例:
<%: func UserList(userList []string, buffer *bytes.Buffer) %><%: func UserList(userList []string, w io.Writer) %><%: func UserList(userList []string, w io.Writer) (int, error) %>
-
模板繼承語句
<%~ "parent template" %>- 該語句聲明要繼承的模板。
- 例:
<%~ "index.html" >
-
模板include語句
<%+ "sub template" %>- 該語句把要include的模板加載進該模板,工作原理和
C++中的#include有點類似。 - 例:
<%+ "user.html" >
- 該語句把要include的模板加載進該模板,工作原理和
-
包導入語句
<%! go code %>- 該語句用來聲明所有在函數外的代碼,包括依賴包導入、全局變量、const等。
- 該語句不會被子模板所繼承
- 例:
<%!
import (
"fmt"
"strings"
)
var a int
const b = "hello, world"
func Add(a, b int) int {
return a + b
}
type S struct {
Name string
}
func (s S) String() string {
return s.Name
}
%>
- 塊語句
<%@ blockName { %> <% } %>
暫不支持該語法,請使用其他方式代替。
- Go代碼語句
<% go code %> - 該語句定義了函數內部的代碼部分。
- 例:
<% for _, user := range userList { %>
<% if user != "Alice" { %>
<%= user %>
<% } %>
<% } %>
<%
a, b := 1, 2
c := Add(a, b)
%>
- 原生值語句
<%==[t] variable %>、<%- variable %> - 該語句把變量轉換為string。
t是變量的類型,hero會自動根據t來選擇轉換函數。t的待選值有:b: booli: int, int8, int16, int32, int64u: byte, uint, uint8, uint16, uint32, uint64f: float32, float64s: stringbs: []bytev: interface
注意:
- 如果
t沒有設置,那麼t默認為s. - 最好不要使用
v,因為其對應的轉換函數為fmt.Sprintf("%v", variable),該函數很慢。 - 例:
<%== "hello" %>
<%==i 34 %>
<%==u Add(a, b) %>
<%==s user.Name %>
- 轉義值語句
<%= statement %> - 該語句把變量轉換為string後,又通過
html.EscapesString記性轉義。 t跟上面原生值語句中的t一樣。- 例:
<%= a %>
<%= a + b %>
<%= Add(a, b) %>
<%= user.Name %>
- 註釋語句
<%# note %> - 該語句註釋相關模板,註釋不會被生成到go代碼裏邊去。
- 例:
<# 這是一個註釋 >.
感謝
Shiyanhui/hero