Make template.Execute write output to a string

24,228 views
Skip to first unread message

Sankar P

unread,
May 2, 2012, 1:43:49 PM5/2/12
to golang-nuts, Sankar P
Hi,

How do I make template.Execute write the generated html to a string
instead of a Writer object ? Is there a StringWriter or some such
class that I can make use of ? If I have to write into some other
writer and then read from it, what writer should I use best for this
scenario ?

Thanks.

--
Sankar P
http://psankar.blogspot.com

sdeg...@8thlight.com

unread,
May 2, 2012, 1:51:55 PM5/2/12
to golan...@googlegroups.com, Sankar P

Sankar P

unread,
May 2, 2012, 2:24:55 PM5/2/12
to sdeg...@8thlight.com, darkh...@darkhelmetlive.com, minu...@gmail.com, golan...@googlegroups.com
Thank you a lot everybody. It works.

For future reference of anyone having the same problem, the code that I've used:

var doc bytes.Buffer
t.Execute(&doc, survey)
s := doc.String()

Thanks.

Russ Cox

unread,
May 2, 2012, 2:58:49 PM5/2/12
to Sankar P, sdeg...@8thlight.com, darkh...@darkhelmetlive.com, minu...@gmail.com, golan...@googlegroups.com
On Wed, May 2, 2012 at 2:24 PM, Sankar P <sankar.c...@gmail.com> wrote:
>   var doc bytes.Buffer
>   t.Execute(&doc, survey)
>   s := doc.String()

It will work even better if you check for an error from t.Execute.

Russ

Sankar P

unread,
May 2, 2012, 3:04:35 PM5/2/12
to r...@golang.org, sdeg...@8thlight.com, darkh...@darkhelmetlive.com, minu...@gmail.com, golan...@googlegroups.com
Thanks. I will be careful.

Wallace Ricardo

unread,
Sep 2, 2014, 4:24:37 PM9/2/14
to golan...@googlegroups.com, r...@golang.org, sdeg...@8thlight.com, darkh...@darkhelmetlive.com, minu...@gmail.com
Thank you!

stian...@gmail.com

unread,
Jun 12, 2015, 8:33:53 AM6/12/15
to golan...@googlegroups.com, r...@golang.org, sdeg...@8thlight.com, minu...@gmail.com, sankar.c...@gmail.com, darkh...@darkhelmetlive.com
hey, 
I looked at this code as it may help me. What is survey here? I mean - i have my template in t that should transfer content to the doc buffer, why do I need the survey?

m4l...@gmail.com

unread,
Oct 7, 2016, 7:45:12 PM10/7/16
to golang-nuts, r...@golang.org, sdeg...@8thlight.com, darkh...@darkhelmetlive.com, minu...@gmail.com
thanks !
Reply all
Reply to author
Forward
0 new messages