2

To enforce a codestyle in a Go project, one can use the gofmt tool to format .go files, and make it part of the build chain.

Is there any possibility to enforce proper formatting of Go template files (usually .html.tmpl extension—the gohtmltmpl filetype on Vim) with a CLI tool, to be used in a continuous integration system?

All the “HTML prettifier” tools I found are not aware of Go templates, and mess up the indenting of such files.

Diti
  • 1,454
  • 3
  • 23
  • 38
  • gohtmltmpl essentially treats Go template items as if they're HTML tags, and you can do the same, but it's nigh impossible to make both the output and the template look good. Generally you want the output to look good, and that's what tools like [Tidy](http://www.html-tidy.org/) do. If you really want the template itself to look good, one solution is to simply convert the `{{` and `}}` strings to something like `` and ``. You might need special handling for `{{-` and `-}}` of course. Then prettify the result, and convert back. I'm unaware of a ready-made solution. –  Sep 09 '17 at 13:44
  • 1
    Note that the approach above will not format things in the same way as gohtmltmpl and it was only meant as an example. As far as I am aware, you'd need to create something yourself that does the same thing as gohtmltmpl. –  Sep 09 '17 at 13:49

0 Answers0