The next code works perfect for output one slice inside the HomeTemplate.
main.go
type Item struct {
Id int
Name string
Type string
}
var tmpl = template.Must(template.ParseGlob("tmpl/*"))
func Index(w http.ResponseWriter, r *http.Request) {
db := database.DbConn()
selDB, err := product.ByID()
if err != nil {
panic(err.Error())
}
i := Item{}
resItems := []Item{}
for selDB.Next() {
var id int
var product_name, product_type string
err = selDB.Scan(&id, &product_name, &product_type)
if err != nil {
panic(err.Error())
}
i.Id = id
i.Name = product_name
i.Type = product_type
resItems = append(resItems, i)
}
tmpl.ExecuteTemplate(w, "HomeTemplate", resItems)
// Close database connection
defer db.Close()
}
In the template forks fine the next code:
{{ range . }}
{{ .Name }}<br />
{{ end }}
Why something like this does not work?
{{ range .resItems }}
{{ .Name }}<br />
{{ end }}
What if I want output two or more slices, what I need to do or change?
Thank you