gpt4 book ai didi

go - 模板不必要地将 `<` 转义为 `<` 但不是 `>`

转载 作者:IT王子 更新时间:2023-10-29 00:36:33 27 4
gpt4 key购买 nike

我在开发一个开发工具,该工具使用模板生成自述文件和许可证等文件。

除了一个 < 实异常(exception),一切正常角色变成了 &lt; - 对应的>字符工作正常并按预期出现在输出中。

模板文件:https://raw.githubusercontent.com/Southclaws/pawn-package-template/master/README.md感兴趣的行是:

```pawn
#include <{{.Repo}}>
```

在哪里Repo被插入,预期的结果是

#include <sometext>

但实际出来的是:

#include &lt;sometext>

我无法从文档中弄清楚为什么会发生这种情况。老实说,这似乎是一个错误,因为如果它正在寻找可以逃脱的东西,它肯定会转 >进入&gt;还有,对吧?

最佳答案

html/template提供自动的、上下文相关的转义以防止代码注入(inject):

HTML templates treat data values as plain text which should be encoded so they can be safely embedded in an HTML document. The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts.

html/template 只是生成 HTML 输出:

It provides the same interface as package text/template and should be used instead of text/template whenever the output is HTML.

如果输出不是 HTML,使用 text/template相反,它不会转义数据。

关于go - 模板不必要地将 `<` 转义为 `&lt;` 但不是 `>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48527115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com