You should simply make a map[string]interface{} containing the issues array and then use that as the template execution data.
Then you can loop over the issues and directly access its' members from the template.
Here's a small complete example:
const t = `
{{ range .issues }}
issue: {{ .ID }}
author: {{ .Author }}
{{ end }}
`
type Issue struct {
ID int
Author string
}
func main() {
issues := []Issue{{1, "Pepe"}, {2, "Cholo"}}
data := map[string]interface{}{"issues": issues}
tpl := template.Must(template.New("bla").Parse(t))
tpl.Execute(os.Stdout, data)
}
Which outputs:
issue: 1
author: Pepe
issue: 2
author: Cholo
Additionally if you want/need to add data specific to the template rendering process you should define a "rich" issue struct for this purpose and transform your model before passing it to template execution. This could be done for both statically known extra data (as simple members of RichIssue) and for dynamically loaded data (as key/values of a map).
Here's an extended example showing the above suggestions:
const t = `
{{ range .issues }}
issue: {{ .ID }}
author: {{ .Author }}
static1: {{ .Static1 }}
dyn1: {{ .Data.dyn1 }}
{{ end }}
`
type Issue struct {
ID int
Author string
}
type RichIssue struct {
Issue
Static1 string // some statically known additional data for rendering
Data map[string]interface{} // container for dynamic data (if needed)
}
func GetIssueStatic1(i Issue) string {
return strconv.Itoa(i.ID) + i.Author // whatever
}
func GetIssueDyn1(i Issue) string {
return strconv.Itoa(len(i.Author)) // whatever
}
func EnrichIssue(issue Issue) RichIssue {
return RichIssue{
Issue: issue,
Static1: GetIssueStatic1(issue),
// in this contrived example I build "dynamic" members from static
// hardcoded strings but these fields (names and data) should come from
// some kind of configuration or query result to be actually dynamic
// (and justify being set in a map instead of being simple static
// members as Static1)
Data: map[string]interface{}{
"dyn1": GetIssueDyn1(issue),
"dyn2": 2,
"dyn3": "blabla",
},
}
}
func EnrichIssues(issues []Issue) []RichIssue {
r := make([]RichIssue, len(issues))
for i, issue := range issues {
r[i] = EnrichIssue(issue)
}
return r
}
func main() {
issues := []Issue{{1, "Pepe"}, {2, "Cholo"}}
data := map[string]interface{}{"issues": EnrichIssues(issues)}
tpl := template.Must(template.New("bla").Parse(t))
tpl.Execute(os.Stdout, data)
}
Which produces the following output:
issue: 1
author: Pepe
static1: 1Pepe
dyn1: 4
issue: 2
author: Cholo
static1: 2Cholo
dyn1: 5