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
+33.7k Golang : All update packages with go get command
+8.4k Your page has meta tags in the body instead of the head
+13.6k Golang : Qt progress dialog example
+19.4k Golang : Fix cannot download, $GOPATH not set error
+13.8k Golang : unknown escape sequence error
+6.7k Golang : Derive cryptographic key from passwords with Argon2
+7.9k Javascript : How to check a browser's Do Not Track status?
+14.6k Golang : Execute function at intervals or after some delay
+12.1k Golang : convert(cast) string to integer value
+20.8k Golang : Underscore or snake_case to camel case example
+32.1k Golang : Validate email address with regular expression
+6k Javascript : Get operating system and browser information