I released a new tool called templify. It intended for use with Google Go. In Go it is a common pattern that your project compiles into a single binary without any external dependencies. Go offers a pretty comprehensive template page. One can define templates as simple strings (as with Printf) or as external template files, that are read at runtime from disk. Both ways have problems:

  • defining a template as a standard string enforces you to deal with a lot of escaping. Additionally either you need to add additional line breaks and string concatenation within your program. Or you need to use multiline strings and your source will get really ugly.
  • reading your template from an external file works nicely, but this breaks the idea of a single binary. The template file will be needed at runtime, updates need to update both files …

So I wrote templify. Using it you can write your template as an external template file. Then you will need to run templify on that file to create a Go source file, that generates a function, which returns the template as a string. So templify kinda invert the file into a Go internal string with correct escaping etc.

templify works nicely with go generate, so that you can include it very comfortably in your Makefile and it will be run at any change of the template file.

Take a look at its Github repository for more details and an example.

For those interested: after I had a working initial version I used templify to internalize its own generating template. That sounds a little confusing (and it was during development :smirk:), but it really nicely cleaned up the source. Using some command-line switches you may provide your own template to be used to create the code.