package html

import "html"

Package html 提供用于转义和反转义 HTML 文本的函数。

Index

Examples

Functions

func EscapeString

func EscapeString(s string) string

EscapeString 将特殊字符(如 "<")转义为 "&lt;"。 它仅转义五个这样的字符:<、>、&、' 和 "。 UnescapeString(EscapeString(s)) == s 始终成立,但反过来并不总是成立。

Example
package main

import (
	"fmt"
	"html"
)

func main() {
	const s = `"Fran & Freddie's Diner" <tasty@example.com>`
	fmt.Println(html.EscapeString(s))
}

Output:

&#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;tasty@example.com&gt;

func UnescapeString

func UnescapeString(s string) string

UnescapeString 将实体(如 "&lt;")反转义为 "<"。它能反转义的实体范围 比 EscapeString 转义的范围更大。例如,"&aacute;" 会被反转义为 "á", "&#225;" 和 "&#xE1;" 也是如此。 UnescapeString(EscapeString(s)) == s 始终成立,但反过来并不总是成立。

Example
package main

import (
	"fmt"
	"html"
)

func main() {
	const s = `&quot;Fran &amp; Freddie&#39;s Diner&quot; &lt;tasty@example.com&gt;`
	fmt.Println(html.UnescapeString(s))
}

Output:

"Fran & Freddie's Diner" <tasty@example.com>

Directories

template Package template (html/template) 实现了数据驱动的模板,用于生成防止代码注入的安全 HTML 输出。