Dealing with empty lines
Template actions (i.e., the code elements placed in a template) may result in unwanted empty spaces and lines. The Go template system offers some mechanisms to deal with these unwanted spaces.
How to do it...
Use -
next to the template delimiter:
{{-
will remove all spaces/tabs/newlines that were output before this template element-}}
will remove all spaces/tabs/newlines that come after this template element
If a template directive produces output, such as the value of a variable, it will be written to the output stream. But if a template directive does not generate any output, such as a {{range}}
or {{if}}
statement, then it will be replaced with empty strings. And if those statements are on a line by themselves, those lines will be written to the output as well, like this:
{{range .}} {{if gt . 1}} {{.}} {{end}} {{end}}
This template will produce an output every four lines. When...