Study Guide: How to add commas between items of a list
{{ range $i, $v := .GetTerms "tags" }}
{{- if $i }}, {{ end }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{- end }}Why this works
The variable $i holds the index value of each slice item (a ‘slice’ in Go-speak is an ‘array’). Because arrays are zero based, the first item has an index of 0. Once all the array items have been parsed, the index is NULL. In both of these cases, the if statement is not TRUE, and so a comma is not displayed. This effectively adds commas in front of all but the first array element.
Related Content
Source: //study/hugo/hugo-commas-in-list-displays/
