html

html

https://pkg.go.dev/html@go1.20.1

Package html provides functions for escaping and unescaping HTML text.

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

常量

This section is empty.

变量

This section is empty.

函数

func EscapeString

1
func EscapeString(s string) string

EscapeString escapes special characters like “<” to become “<”. It escapes only five such characters: <, >, &, ’ and “. UnescapeString(EscapeString(s)) == s always holds, but the converse isn’t always true.

EscapeString用于转义HTML文本中的特殊字符,例如将”<“转义为”<"。它只转义五个特殊字符: <, >, &, ’ 和 “。UnescapeString(EscapeString(s)) == s 总是成立,但反过来不一定成立。

EscapeString Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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

1
func UnescapeString(s string) string

UnescapeString unescapes entities like “<” to become “<”. It unescapes a larger range of entities than EscapeString escapes. For example, “á” unescapes to “á”, as does “á” and “á”. UnescapeString(EscapeString(s)) == s always holds, but the converse isn’t always true.

UnescapeString用于反转义实体,例如将”<“反转义为”<"。它比EscapeString转义更多的实体。例如,“á"反转义为"á”,“á"和"á"也是如此。UnescapeString(EscapeString(s)) == s 总是成立,但反过来不一定成立。

UnescapeString Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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>

类型

This section is empty.