引き続きgolang。
package main import ( "encoding/json" "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World.") } func JsonHandler(w http.ResponseWriter, r *http.Request) { var body = map[string]int{ "a": 0, "b": 1, "c": 2, "d": 3, } _json, err := json.Marshal(body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.Write(_json) } func main() { http.HandleFunc("/", handler) http.HandleFunc("/hello.json", JsonHandler) http.ListenAndServe(":15100", nil) }
/hello.jsonへのリクエストで呼ばれるJsonHandlerが追加された部分。mapの使い方はこれでいいんだろうか。Pythonの辞書みたいにキーによってデータの型が違うっていうのは書けないのかなぁ。