This is my testing code.Just make a simple http server. Then generating a json data that it's value is "&". But the result is what i don't want. The result is below the code block.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
func testFunc(w http.ResponseWriter, r *http.Request) {
data := make(map[string]string)
data["key"] = "&"
bytes, err := json.Marshal(data)
if err != nil {
fmt.Fprintln(w, "generator json error")
} else {
//print console
fmt.Println(string(bytes))
fmt.Println("&")
//print broswer
fmt.Fprintln(w, string(bytes))
fmt.Fprintln(w, "&")
}
}
func main() {
http.HandleFunc("/", testFunc)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe", err)
}
}
result: Chrome browser show:
{"key":"\u0026"}
&
Console also show:
{"key":"\u0026"}
&
When '&' not in json, browser and console will print '&'.