Trying to execute a template, and store the result in a data base. Right now, the data gets stored, but not in the form of the template I wanted.
type Data struct {
Msg string
Name string
}
type DataDB struct {
ID int
Data
}
func main() {
justin := DataDB{ID:1, Data: Data {Msg: "Justin", Name: "ABC"}}
ruben := DataDB{ID:2, Data: Data {Msg: "Ruben", Name: "tratata"}}
petyaT := DataDB{ID:1, Data: Data {Msg: "Petya Tereodor Pidgallo", Name: "ololol"}}
datas := []DataDB{justin, ruben, petyaT}
tpl, err := template.New("msgs").Parse(` {{range .}}
Hello {{.Data.Msg}}, my name is {{.Data.Name}}
{{end}}
`)
if err != nil {
panic(err)
}
err = tpl.Execute(os.Stdout, datas)
if err != nil {
log.Fatalln(err)
}
for _, v := range datas {
s := []string{v.Data.Msg,v.Data.Name}
_, err = db.Exec("INSERT INTO datadb (id, data) VALUES($1, $2)", v.ID, pq.Array(s))
if err != nil {
panic(err)
}
}
}
I executed the template to stdout and the results I got, were correct -
Hello Justin, my name is ABC
Hello Ruben, my name is tratata
Hello Petya Tereodor Pidgallo, my name is ololol.
But in my db, I got -
1"{“Justin”,“ABC”}"
2"{“Ruben”,“tratata”}"
1"{“Petya Tereodor Pidgallo”,“ololol”}"
Im trying to execute the template to my db(store the result after I execute my template, to the db).
Hope I explained correctly. Thanks in advance.