Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/valyala/fasttemplate
Simple and fast template engine for Go
https://github.com/valyala/fasttemplate
fast go golang placeholder template
Last synced: 1 day ago
JSON representation
Simple and fast template engine for Go
- Host: GitHub
- URL: https://github.com/valyala/fasttemplate
- Owner: valyala
- License: mit
- Created: 2015-08-19T12:44:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-08-28T10:30:11.000Z (over 1 year ago)
- Last Synced: 2024-10-29T21:55:47.889Z (about 1 month ago)
- Topics: fast, go, golang, placeholder, template
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 839
- Watchers: 22
- Forks: 80
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - fasttemplate - Simple and fast template engine. Substitutes template placeholders up to 10x faster than [text/template](https://golang.org/pkg/text/template/). (Template Engines / HTTP Clients)
- zero-alloc-awesome-go - fasttemplate - Simple and fast template engine. Substitutes template placeholders up to 10x faster than [text/template](https://golang.org/pkg/text/template/). (Template Engines / HTTP Clients)
- go-awesome - fasttemplate - Simple and fast template engine for Go (Open source library / Template Engine)
- awesome-go - fasttemplate - Simple and fast template engine for Go - ★ 226 (Template Engines)
- awesome-go-extra - fasttemplate - 08-19T12:44:22Z|2021-01-11T18:21:27Z| (Template Engines / HTTP Clients)
README
fasttemplate
============Simple and fast template engine for Go.
Fasttemplate performs only a single task - it substitutes template placeholders
with user-defined values. At high speed :)Take a look at [quicktemplate](https://github.com/valyala/quicktemplate) if you need fast yet powerful html template engine.
*Please note that fasttemplate doesn't do any escaping on template values
unlike [html/template](http://golang.org/pkg/html/template/) do. So values
must be properly escaped before passing them to fasttemplate.*Fasttemplate is faster than [text/template](http://golang.org/pkg/text/template/),
[strings.Replace](http://golang.org/pkg/strings/#Replace),
[strings.Replacer](http://golang.org/pkg/strings/#Replacer)
and [fmt.Fprintf](https://golang.org/pkg/fmt/#Fprintf) on placeholders' substitution.Below are benchmark results comparing fasttemplate performance to text/template,
strings.Replace, strings.Replacer and fmt.Fprintf:```
$ go test -bench=. -benchmem
PASS
BenchmarkFmtFprintf-4 2000000 790 ns/op 0 B/op 0 allocs/op
BenchmarkStringsReplace-4 500000 3474 ns/op 2112 B/op 14 allocs/op
BenchmarkStringsReplacer-4 500000 2657 ns/op 2256 B/op 23 allocs/op
BenchmarkTextTemplate-4 500000 3333 ns/op 336 B/op 19 allocs/op
BenchmarkFastTemplateExecuteFunc-4 5000000 349 ns/op 0 B/op 0 allocs/op
BenchmarkFastTemplateExecute-4 3000000 383 ns/op 0 B/op 0 allocs/op
BenchmarkFastTemplateExecuteFuncString-4 3000000 549 ns/op 144 B/op 1 allocs/op
BenchmarkFastTemplateExecuteString-4 3000000 572 ns/op 144 B/op 1 allocs/op
BenchmarkFastTemplateExecuteTagFunc-4 2000000 743 ns/op 144 B/op 3 allocs/op
```Docs
====See http://godoc.org/github.com/valyala/fasttemplate .
Usage
=====```go
template := "http://{{host}}/?q={{query}}&foo={{bar}}{{bar}}"
t := fasttemplate.New(template, "{{", "}}")
s := t.ExecuteString(map[string]interface{}{
"host": "google.com",
"query": url.QueryEscape("hello=world"),
"bar": "foobar",
})
fmt.Printf("%s", s)// Output:
// http://google.com/?q=hello%3Dworld&foo=foobarfoobar
```Advanced usage
==============```go
template := "Hello, [user]! You won [prize]!!! [foobar]"
t, err := fasttemplate.NewTemplate(template, "[", "]")
if err != nil {
log.Fatalf("unexpected error when parsing template: %s", err)
}
s := t.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
switch tag {
case "user":
return w.Write([]byte("John"))
case "prize":
return w.Write([]byte("$100500"))
default:
return w.Write([]byte(fmt.Sprintf("[unknown tag %q]", tag)))
}
})
fmt.Printf("%s", s)// Output:
// Hello, John! You won $100500!!! [unknown tag "foobar"]
```