Golang中如何同时使用"html/template"和"text/template"

How to use "html/template" and "text/template" at the same time in Golang

我正在使用 Go 发送电子邮件,我正在从存储在数据库中的模板中解析主题和 HTML 正文。

对于主题,我不希望 t.Parse() 转义 html 实体,因为它不是 HTML,只是纯文本,但对于主体而言,这正是行为我要。

如何在同一个文件中执行这两项操作?

我假设您面临的问题是同名 template 包之间的导入冲突。

在这种情况下,对一个或两个包使用命名导入,以通过源文件中的另一个名称访问其成员:

package mypackage

import (
    htmltemplate "html/template"
    texttemplate "text/template"
)

现在像往常一样编写引用模板包的代码,但使用别名 htmltemplatetexttemplate 代替 template