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
+17k Golang : Covert map/slice/array to JSON or XML format
+6.9k Golang : Pat multiplexer routing example
+6.7k Golang : Experimental emojis or emoticons icons programming language
+22.2k Golang : Securing password with salt
+5.1k Swift : Convert (cast) Float to Int or Int32 value
+22.4k Golang : Read directory content with filepath.Walk()
+9.9k Golang : Turn string or text file into slice example
+10.4k Golang : Generate random integer or float number
+9k Golang : Build and compile multiple source files
+14.3k Golang : Get uploaded file name or access uploaded files
+14.5k Golang : Find network of an IP address
+55.3k Golang : Unmarshal JSON from http response