# Executing template to a database

**URL:** <https://forum.golangbridge.org/t/executing-template-to-a-database/16792>\
**Category:** Getting Help\
**Created:** [December 25, 2019, 11:15am UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792 "2019-12-25T11:15:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JustinObanor](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/justinobanor/32/8075_2.png) [@JustinObanor](https://forum.golangbridge.org/u/JustinObanor)\
**Post date:** [December 25, 2019, 11:15am UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/1 "2019-12-25T11:15:41Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![lutzhorn](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lutzhorn/32/8119_2.png) [@lutzhorn](https://forum.golangbridge.org/u/lutzhorn)\
**Post date:** [December 25, 2019, 7:04pm UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/2 "2019-12-25T19:04:28Z")

</div>

> [@JustinObanor](#):
>
> ```auto
> err = tpl.Execute(os.Stdout, datas)
> 
> ```

You execute the template and write the result to STDOUT. Then, later you write the string representation of each `DataDB` into the database. If you want to store the result of executing the template to the database, you should make `s` be the result of executing the template with a `DataDB`.

---

<div class="post-metadata">

**Author:** ![JustinObanor](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/justinobanor/32/8075_2.png) [@JustinObanor](https://forum.golangbridge.org/u/JustinObanor)\
**Post date:** [December 25, 2019, 7:06pm UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/3 "2019-12-25T19:06:30Z")

</div>

Yeah, the things that im not sure how to make “s” be the result of the executed template

---

<div class="post-metadata">

**Author:** ![lutzhorn](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lutzhorn/32/8119_2.png) [@lutzhorn](https://forum.golangbridge.org/u/lutzhorn)\
**Post date:** [December 25, 2019, 7:10pm UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/4 "2019-12-25T19:10:27Z")

</div>

Try something like this when using [`Template.Execute`](https://golang.org/pkg/text/template/#Template.Execute):

```auto
var tpl bytes.Buffer
if err := t.Execute(&tpl, data); err != nil {
    return err
}

result := tpl.String()

```

**Edit:** Your code could look like this:

```auto
    tpl, err := template.New("msgs").Parse(`Hello {{.Data.Msg}}, my name is {{.Data.Name}}`)
	if err != nil {
		panic(err)
	}

	for _, v := range datas {
		var buf bytes.Buffer
		if err := tpl.Execute(&buf, v); err != nil {
			log.Fatalln(err)
		}

		s := buf.String()
		fmt.Println(s)
		// Insert s into the database table.
	}

```

see [https://play.golang.org/p/iMI0ubaAm62](https://play.golang.org/p/iMI0ubaAm62)

---

<div class="post-metadata">

**Author:** ![geosoft1](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/geosoft1/32/2151_2.png) [@geosoft1](https://forum.golangbridge.org/u/geosoft1)\
**Post date:** [December 25, 2019, 8:23pm UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/5 "2019-12-25T20:23:35Z")

</div>

> [@JustinObanor](#):
>
> But in my db, I got -  
> 1"{“Justin”,“ABC”}"

What type is the column data? Because it looks like you try to write into a JSON type column in database that can explain the output format.

---

<div class="post-metadata">

**Author:** ![JustinObanor](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/justinobanor/32/8075_2.png) [@JustinObanor](https://forum.golangbridge.org/u/JustinObanor)\
**Post date:** [December 25, 2019, 8:32pm UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/7 "2019-12-25T20:32:07Z")

</div>

Its of type character varying. Basically a string

---

<div class="post-metadata">

**Author:** ![JustinObanor](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/justinobanor/32/8075_2.png) [@JustinObanor](https://forum.golangbridge.org/u/JustinObanor)\
**Post date:** [December 26, 2019, 12:41pm UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/8 "2019-12-26T12:41:50Z")

</div>

Thanks, that did the trick

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [March 25, 2020, 12:41pm UTC](https://forum.golangbridge.org/t/executing-template-to-a-database/16792/9 "2020-03-25T12:41:53Z")

</div>

Closed on March 25, 2020, 12:41pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
