Skip to content

Instantly share code, notes, and snippets.

@shouhei
Created March 13, 2017 13:02
Show Gist options
  • Save shouhei/da8b6238383993a4c17665cc28e68e13 to your computer and use it in GitHub Desktop.
Save shouhei/da8b6238383993a4c17665cc28e68e13 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"text/template"
)
type Hoge struct {
Count int
Material string
}
type T struct {
Data []Hoge
}
func main() {
sweaters := []Hoge{Hoge{1, "hogehuga"}, Hoge{2, "hugahuga"}, Hoge{3, "piyopiyo"}}
tmpl, err := template.New("test").Parse(`{{range $_, $hoge := .Data}}{{$hoge.Count}} items are made of {{$hoge.Material}}
{{end}}`)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, T{sweaters})
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment