I want to generate go template with loop which looks like following
target1:
echo from target2
target2:
echo from target2
targetN:
echo from targetN
This simply creates a target and line after it prints from which target it's called.
I've tried with the following which works partially, any Idea what I miss here ? or how can I simplify the code in t.Execute
https://play.golang.org/p/iLWQANobKUL
package main
import (
"bytes"
"fmt"
"html/template"
)
func main() {
t := template.Must(template.New("").Parse(` {{- range .}} {{.Name}}:{{end}}
{{- range .}}
{{.Name}}:
{{"\t"}}echo from {{.Name}} {{.Text}}
{{end}}
`))
var buf bytes.Buffer
t.Execute(&buf, []interface{}{map[string]string{"Name": "target1", "Text": "echo"}})
fmt.Printf("%q
", buf.String())
}