在“go”中执行模板(在我的例子中为“tmpl html”)很容易操作系统标准输出但如何将其写入字符串“输出”,以便稍后我可以使用以下方式在邮件中发送 html"gopkg.in/gomail.v2"
?
var output string
t := template.Must(template.New("html table").Parse(tmplhtml))
err = t.Execute(output, Files)
m.SetBody("text/html", output) //"gopkg.in/gomail.v2"
构建错误读取'不能在 t.Execute 的参数中使用输出(字符串类型)作为 io.Writer 类型:字符串未实现 io.Writer(缺少 Write 方法)'我可以实现 Writer 方法,但它应该返回整数Write(p []byte) (n int, err 错误)
您需要按如下方式写入缓冲区,因为这实现了接口io.Writer
。它基本上缺少一个 Write 方法,您可以构建自己的方法,但缓冲区更直接:
buf := new(bytes.Buffer)
t := template.Must(template.New("html table").Parse(tmplhtml))
err = t.Execute(buf, Files)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)