kuda.ai

Thoughts on programming and music theory.

dark mode

Go Template Whitespace Trimming

Created on April 4, 2026

go/templates

What follows is an example that shows different options with whitespace trimming. I never quite understood it, so here is a showcase as reference.

v1:

This template:

{{- end -}}
<label>
  <strong>Date:</strong>
  <input
    name="date"
    type="date"
    {{ if .ViewModels.Activity }}
    value="{{- .ViewModels.Activity.Date | formatDateFormInput -}}"
    {{ else }}
    value="{{- .Today | formatDateFormInput -}}"
    {{ end }}
  />
</label>

Becomes:

</h2><label>
   <strong>Date:</strong>
   <input
     name="date"
     type="date"
     
     value="2026-02-16"
     
   />
</label>

v2:

rm first minus in the end block:

This template:

{{- end }}
<label>
  <strong>Date:</strong>
  <input
    name="date"
    type="date"
    {{ if .ViewModels.Activity }}
    value="{{- .ViewModels.Activity.Date | formatDateFormInput -}}"
    {{ else }}
    value="{{- .Today | formatDateFormInput -}}"
    {{ end }}
  />
</label>

Becomes:

<label>
  <strong>Date:</strong>
  <input
    name="date"
    type="date"
    
    value="2026-02-16"
    
  />
</label>

v3:

add minuses in the if else blocks:

This template:

{{- end }}
<label>
  <strong>Date:</strong>
  <input
    name="date"
    type="date"
    {{- if .ViewModels.Activity -}}
    value="{{- .ViewModels.Activity.Date | formatDateFormInput -}}"
    {{- else -}}
    value="{{- .Today | formatDateFormInput -}}"
    {{- end -}}
  />
</label>

Becomes:

<label>
  <strong>Date:</strong>
  <input
    name="date"
    type="date"value="2026-02-16"/>
</label>

v4:

try to use them in a way that produces a nice coherent result:

This template:

{{- end }}
<label>
  <strong>Date:</strong>
  <input
    name="date"
    type="date"
    {{ if .ViewModels.Activity -}}
    value="{{- .ViewModels.Activity.Date | formatDateFormInput -}}"
    {{- else -}}
    value="{{- .Today | formatDateFormInput -}}"
    {{- end }}
  />
</label>

Becomes:

<label>
  <strong>Date:</strong>
  <input
    name="date"
    type="date"
    value="2026-02-16"
  />
</label>