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
+17.8k Golang : Iterate linked list example
+25.5k Golang : Generate MD5 checksum of a file
+12.3k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+13.2k Golang : Convert(cast) int to int64
+5.4k Golang : What is StructTag and how to get StructTag's value?
+8.7k Golang : Find duplicate files with filepath.Walk
+12.7k Golang : Pass database connection to function called from another package and HTTP Handler
+8.1k Golang : Randomize letters from a string example
+10.3k Golang : Embed secret text string into binary(executable) file
+23.9k Golang : Use regular expression to validate domain name