14

Loving Go's built-in template libraries, currently I am just declaring the template as const string. How does one normally go about including larger more sophisticated template files? Ideally I prefer them to be inside the binary to simplify deployment.

2
  • 3
    Do you mean like go-bindata or go.rice? (stackoverflow.com/q/12482311/6309)
    – VonC
    May 18, 2014 at 12:07
  • 1
    maybe? They look like third party utilities. Does that mean that theres no standardised way? (Im a little surprised if that is the case)
    – Jay
    May 18, 2014 at 12:12

4 Answers 4

15

Embedding static files in 2021 has become a bit easier since the release of Go 1.16. The new release comes with a new package embed which provides a handy set of interface and methods to attach static file in go binaries

go version
# 1.16.x

# then
go doc embed

example in cks-cli software

1
10

Historically there was no standard way to do this in Go. This answer is preserved for historical reasons. See below for updated answer.

--

As comments show there is a few libraries available that will help you transform binary data (like templates, images eg.) to Go source files that can be compiled with your own source files to the final binary.

Although this approach works for many projects I will recommend you reconsider. The cost of having easy distribution is that you must re-generate the source files creating assets before compiling the main source code and when you want to distribute a minor change to the templates/javascript/images&eg. included this way you will have to re-compile and restart the whole server.

On most projects I've worked on changes in frontend stuff is by far the most frequent kind of change - which caused us to move away from this practice.

5
  • 1
    True, but I’d rather have only one file that I need to deploy/push onto each server, and when it’s all in one file it’s easier to roll back to an earlier release. Having to edit and push/sync a collection of template files sounds quite inconvenient. How do you do that, raunchy perhaps. Easier to bundle it all in one file I would argue.
    – Jay
    Feb 10, 2018 at 21:04
  • @Jacob I'd argue you need to fix your deployment process, not your builds. Keep the last few deploys on your server, and have a symlink to the current one. If you want to rollback, just point the symlink back. Very quick. Otherwise, rsync will make short work of of syncing a lot of files.
    – mpen
    Apr 15, 2018 at 17:08
  • @mpen I’ve spent years doing it the way you describe, and my personal experience is, if you weigh all the pros and cons, your proposal is more error prone, for a whole list of reasons there is no space to enumerate here. I encourage you to considee branching out and trying it.
    – Jay
    Apr 16, 2018 at 21:27
  • @mpen Excited about the go file embedding in 1.16. It seems legit enough that its now a feature of the go language itself. If I can prevent one more temptation for people going in and hacking at html on production servers, I will do it 😀 Did you ever try it/change your mind?
    – Jay
    Jul 22, 2021 at 4:55
  • 1
    @Jay Yes and no. I use Kubernetes now. So my whole app is "one binary" in the sense that it's compiled into a single image now. And there are indeed benefits to it, namely you can scale up and down and rebuild your server in a heartbeat without fiddling with files. So I'd argue it goes even further than just a Go binary, since it includes the whole server stack, configuration and everything.
    – mpen
    Jul 22, 2021 at 6:26
4

packr is similar to go-bindata, but actively maintained and maybe even a little nicer to use:

https://github.com/gobuffalo/packr

2

Here's a solution that fulfills the need and prevents the hardship described by mbazon:

https://godoc.org/github.com/go-bindata/go-bindata

It compiles to binary, but when you're in "debug" mode, it reads your static assets directly from the disk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.