Skip to content

Instantly share code, notes, and snippets.

@DavadDi
Created February 19, 2017 09:04
Show Gist options
  • Save DavadDi/229f554f6a4a86e40df5e19f077b0f51 to your computer and use it in GitHub Desktop.
Save DavadDi/229f554f6a4a86e40df5e19f077b0f51 to your computer and use it in GitHub Desktop.
html_template.go
package main
import (
"html/template"
"os"
)
type Class struct {
Name string
Value int32
}
type EntetiesClass struct {
Name string
Cls Class
}
// In the template, we use rangeStruct to turn our struct values
// into a slice we can iterate over
const htmlTemplate = `
{{range .}}
{{.Name}} {{.Cls.Name}} {{.Cls.Value}}
{{end}}
`
func main() {
data := []EntetiesClass{
{"Yoga1", Class{"CLA1", 12}}, {"Yoga2", Class{"CLA2", 15}},
}
t := template.New("t")
t, err := t.Parse(htmlTemplate)
if err != nil {
panic(err)
}
err = t.Execute(os.Stdout, data)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment