This is my testing code. Just make a simple HTTP server. Then generating a JSON data that it values 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 &
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…