 <p>Instead of appending the data, generate the output with one execution of the template:</p> <pre><code>package main import ( "fmt" "os" "text/template" ) var t = template.Must(template.New("").Parse(` type Client struct { Opts *ClientOpts Schemas *Schemas Types map[string]Schema {{range .}} Container *{{.schema.Id}}Client{{end}} } `)) type schema struct { Id string } func main() { data := []map[string]interface{}{ {"schema": schema{Id: "abcClient"}}, {"schema": schema{Id: "xyzClient"}}, } if err := t.Execute(os.Stdout, data); err != nil { fmt.Println(err) } } </code></pre> <p><a href="http://play.golang.org/p/T903Y8I77G" rel="nofollow">Try it on the playground</a>.</p>