golang template.JS 到字符串和 template.HTML 到字符串

golang template.JS to string and template.HTML to string

我正在使用 "html/template"。

如何将 template.JS 对象的值转换为字符串? template.HTML呢?

我有一个结构,用于存储一些 js 脚本和 css code/files,例如:"HeaderCSS"、"FooterJS"。我将它们加载到模板的 header/footer 中。问题是我需要检查它们是否已加载,所以我不会加载相同的脚本两次(每次重新加载意味着一个新的脚本加载),因为我正在使用追加,以便能够插入多个脚本。这样,每次刷新都会将脚本附加到现有脚本中。

我的想法是检查它们是否已经加载,以防止多次加载它们。我正在考虑使用 strings.Contains() 函数。但我不能这样做,因为我不知道将 templates.HTML 和 templates.JS 转换为字符串。

有什么想法吗?

Per the documentation, they're strings。只需键入 cast to string:

asStr := string(myTplJsObj)