|
@@ -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)
|
|
|
+}
|