Saturday, January 8, 2022

How To Format Go Templates

I found almost nothing on how to format Go templates so here's a quick guide.

  1. Create your templates, I'm using the <name>.<type>.html notation, the <type> is one of page, layout or partial
  2. Ensure prettier is installed globally as well as a plugin to make prettier work with Go templates
npm i -g prettier prettier-plugin-go-template
  1. Add this .prettierrc to the root of your project
{
  "overrides": [
    {
      "files": ["*.html"],
      "options": {
        "parser": "go-template"
      }
    }
  ]
}
  1. Run the prettier write command whenever you want to format your templates
prettier -w .

the . represents the current directory