I need to use the *template.Execute method but I want the result as a string or byte[] so that I can pass it to another *template.Execute but the method writes its results to a writer. Is there a way to create a writer that will write to a variable I define?
Use an instance of bytes.Buffer
, which implements io.Writer
:
var buff bytes.Buffer
if err := tpl.Execute(&buff, data); err != nil {
panic(err)
}
You can then get a string
result using buff.String()
, or a []byte
result using buff.Bytes()
.