How to use go-bindata with html/template
📅
Mon, Mar 27, 2017
⏱️
One-minute read
What is go-bindata and why do we need it?
go-bindata converts any text or binary file into Go source code, which is useful for embedding data into Go programs. So you can build your whole project into 1 binary file for easier delivery.
html/template
html/template’s functions Parse
, ParseFiles
works only with files on the filesystem, so we need to implement a port to work with both approaches: files or go-bindata.
Pull sample code to play with:
|
|
|
|
Generate templates with go-bindata
We need to install go-bindata CLI and generate a .go file from our templates:
|
|
I prefer to add last command to go:generate
:
|
|
Use go-bindata templates
I made it by providing a flag -go-bindata
:
|
|
Conclusion
- With
go-bindata
you can simplify your deployment with only one binary file. go-bindata
can give you a little faster templates reading.- Note that if you use
ParseFiles
you have to change it to work withAssert
function.