dtny30176
2016-05-09 20:32
浏览 146
已采纳

go不能在模板Execute的参数中将输出(字符串类型)用作io.Writer类型

It's easy to execute template ('tmplhtml' in my case) in 'go' to os.Stdout but how to write it to a string 'output' so I can later i.e. send html in mail using "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"

Build error reads 'cannot use output (type string) as type io.Writer in argument to t.Execute: string does not implement io.Writer (missing Write method)' I can implement Writer method but it is supposed to return integer Write(p []byte) (n int, err error)

图片转代码服务由CSDN问答提供 功能建议

很容易在“ ”操作系统中执行模板(在我的情况下为“ tmplhtml”)。 ,但是如何将其写入字符串“输出”,以便以后可以使用“ gopkg.in/gomail.v2” 在邮件中发送html?

  var输出字符串
t:= template.Must(template.New(“ html table”)。Parse(tmplhtml))
 err = t.Execute(输出,文件)
m.SetBody(“  text / html“,输出)//” gopkg.in/gomail.v2"
  
 
 

生成错误读取'不能将输出(类型字符串)用作 在t.Execute的参数中键入io.Writer。字符串不实现io.Writer(缺少Write方法)'我可以实现Writer方法,但应该返回整数 Write(p [] byte) (n int,错误)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtv7174 2016-05-09 20:36
    已采纳

    You need to write to a buffer as follows as this implements the interface io.Writer. Its basically missing a Write method, which you could build your own, but a buffer is more straight forward:

    buf := new(bytes.Buffer)
    t := template.Must(template.New("html table").Parse(tmplhtml))
    err = t.Execute(buf, Files)
    
    打赏 评论

相关推荐 更多相似问题