https://github.com/plutov/go-bindata-tpl
How to use go-bindata with html/template
https://github.com/plutov/go-bindata-tpl
Last synced: about 1 month ago
JSON representation
How to use go-bindata with html/template
- Host: GitHub
- URL: https://github.com/plutov/go-bindata-tpl
- Owner: plutov
- License: mit
- Created: 2017-03-27T05:36:34.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-27T07:28:31.000Z (about 8 years ago)
- Last Synced: 2025-01-30T15:45:00.920Z (3 months ago)
- Language: Go
- Size: 4.88 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#### What is go-bindata and why do we need it?
[go-bindata](https://github.com/jteeuwen/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](https://golang.org/pkg/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. Files:
```
go build && ./go-bindata-tplHello
```
#### Generate templates with go-bindata
We need to install go-bindata CLI and generate a .go file from our templates:
```
go get -u github.com/jteeuwen/go-bindata/...
go-bindata -o tpl.go tpl
```I prefer to add last command to `go:generate`:
```
//go:generate go-bindata -o tpl.go tpl
```#### Use go-bindata templates
I made it by providing a flag `-go-bindata`:
```
./go-bindata-tpl -go-bindataHello
```
#### 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 with `Assert` function.