我正在嘗試使用Go例程將Json數據返回給請求。當我test1(w,r)沒有「去」我的代碼工作。當我使用test1()作爲去例行程序時,我沒有收到任何json數據。這是爲什麼發生?否Json返回
func main() {
http.HandleFunc("/test", viewdata)
http.ListenAndServe(":8080", nil)
}
func viewdata(w http.ResponseWriter, r *http.Request) {
go test1(w, r)
}
func test1(w http.ResponseWriter, r *http.Request) {
// example struct
ll := sample{
"city",
12,
}
w.Header().Set("Content-Type", "application/json")
json, _ := json.Marshal(ll)
w.Write(json)
}