GO語言"encoding/json"包中"HTMLEscape"函數的用法及代碼示例。
用法:
func HTMLEscape(dst *bytes.Buffer, src []byte)
HTMLEscape 將 JSON-encoded src 附加到 dst,其中字符串文字中的 <、>、&、U+2028 和 U+2029 字符更改為 \u003c、\u003e、\u0026、\u2028、\u2029,以便 JSON 是安全的嵌入 HTML <script> 標簽。由於曆史原因,Web 瀏覽器不支持 <script> 標記內的標準 HTML 轉義,因此必須使用替代的 JSON 編碼。
例子:
package main
import (
"bytes"
"encoding/json"
"os"
)
func main() {
var out bytes.Buffer
json.HTMLEscape(&out, []byte(`{"Name":"<b>HTML content</b>"}`))
out.WriteTo(os.Stdout)
}
輸出:
{"Name":"\u003cb\u003eHTML content\u003c/b\u003e"}
相關用法
- GO HandleFunc用法及代碼示例
- GO Hijacker用法及代碼示例
- GO HasPrefix用法及代碼示例
- GO Handle用法及代碼示例
- GO HasSuffix用法及代碼示例
- GO PutUvarint用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO LeadingZeros32用法及代碼示例
- GO NewFromFiles用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Time.Sub用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO Encode用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO Value用法及代碼示例
- GO StreamWriter用法及代碼示例
- GO Fscanln用法及代碼示例
- GO Values.Get用法及代碼示例
- GO NumError用法及代碼示例
- GO TrailingZeros8用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 HTMLEscape。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。