Kaynağa Gözat

Update hello.go

more efficient marshalling and printing
John Asmuth 12 yıl önce
ebeveyn
işleme
0860df3d6a
1 değiştirilmiş dosya ile 3 ekleme ve 3 silme
  1. 3 3
      go/hello.go

+ 3 - 3
go/hello.go

@@ -13,12 +13,12 @@ type MessageStruct struct {
 
 func hello(w http.ResponseWriter, r *http.Request) {
   m := MessageStruct{"Hello, world"}
-  j, _ := json.Marshal(m)
-  fmt.Fprintf(w, string(j))
+  enc := json.NewEncoder(w)
+  enc.Encode(m)
 }
 
 func main() {
   runtime.GOMAXPROCS(runtime.NumCPU())
   http.HandleFunc("/json", hello)
   http.ListenAndServe(":8080", nil)
-}
+}