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
+8k Golang : Handle Palindrome string with case sensitivity and unicode
+17.6k Golang : delete and modify XML file content
+7.6k Javascript : Push notifications to browser with Push.js
+12.8k Golang : http.Get example
+20.9k Golang : Underscore or snake_case to camel case example
+11.5k Golang : Handle API query by curl with Gorilla Queries example
+19.1k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+9.9k Golang : Translate language with language package example
+13.7k Golang : Image to ASCII art example
+43.5k Golang : Get hardware information such as disk, memory and CPU usage
+17.5k Golang : Clone with pointer and modify value
+7k Golang : Takes a plural word and makes it singular