Browse Source

Make Go go faster.

tarndt 12 years ago
parent
commit
8400125262
1 changed files with 13 additions and 12 deletions
  1. 13 12
      go/hello.go

+ 13 - 12
go/hello.go

@@ -1,24 +1,25 @@
 package main
 
 import (
-  "net/http";
-  "encoding/json";
-  "runtime";
-  "fmt";
+	"encoding/json"
+	"net/http"
+	"runtime"
+	"strconv"
 )
 
 type MessageStruct struct {
-    Message string
+	Message string
 }
 
 func hello(w http.ResponseWriter, r *http.Request) {
-  m := MessageStruct{"Hello, world"}
-  j, _ := json.Marshal(m)
-  fmt.Fprintf(w, string(j))
+	w.Header().Set("Content-Type", "application/javascript")
+	j, _ := json.Marshal(&MessageStruct{"Hello, world"})
+	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
+	w.Write(j)
 }
 
 func main() {
-  runtime.GOMAXPROCS(runtime.NumCPU())
-  http.HandleFunc("/json", hello)
-  http.ListenAndServe(":8080", nil)
-}
+	runtime.GOMAXPROCS(runtime.NumCPU())
+	http.HandleFunc("/json", hello)
+	http.ListenAndServe(":8080", nil)
+}