Golang encoding/json.HTMLEscape() function example
package encoding/json
HTMLEscape appends to dst(1st parameter) the JSON-encoded src(2nd parameter) with
<
,>
,&
,U+2028
andU+2029
characters inside string literals changed to\u003c
,\u003e
,\u0026
,\u2028
,\u2029
so that the JSON will be safe to embed inside HTML<script>
tags. For historical reasons, web browsers don't honor standard HTML escaping within<script>
tags, so an alternative JSON encoding must be used.
Golang encoding/json.HTMLEscape() function usage example
package main
import (
"bytes"
"encoding/json"
"fmt"
)
func main() {
dst := new(bytes.Buffer)
src := []byte(`{
"<script>Name":"Adam Ng", // <----- look here
"Age":36,
"Job":"CEO"
}`)
json.HTMLEscape(dst, src)
fmt.Println(dst)
}
Output :
{
"\u003cscript\u003eName":"Adam Ng",
"Age":36,
"Job":"CEO"
}
Reference :
Advertisement
Something interesting
Tutorials
+10.5k Golang : Create matrix with Gonum Matrix package example
+6.3k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+9.9k Golang : Check if user agent is a robot or crawler example
+9.7k Golang : Eroding and dilating image with OpenCV example
+15.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+15.9k Golang : Get file permission
+18.8k Golang : Implement getters and setters
+5k Golang : Constant and variable names in native language
+17.9k Golang : Qt image viewer example
+7.3k Golang : How to convert strange string to JSON with json.MarshalIndent
+19.3k Golang : Get RGBA values of each image pixel