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
+7.2k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+30k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+5.6k PHP : Fix Call to undefined function curl_init() error
+11.6k Golang : Display a text file line by line with line number example
+29.4k Golang : JQuery AJAX post data to server and send data back to client example
+18.5k Golang : Set, Get and List environment variables
+8k Findstr command the Grep equivalent for Windows
+8.1k Golang : Randomize letters from a string example
+21.6k Golang : GORM create record or insert new record into database example
+14.6k Golang : How to get URL port?
+9.7k Random number generation with crypto/rand in Go
+22.7k Golang : Set and Get HTTP request headers example