{"id":2453,"date":"2018-10-02T06:11:39","date_gmt":"2018-10-01T21:11:39","guid":{"rendered":"http:\/\/okamurax.com\/?p=2453"},"modified":"2020-05-06T00:18:02","modified_gmt":"2020-05-05T15:18:02","slug":"golang%e8%89%b2%e3%80%85","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2453","title":{"rendered":"golang\u8272\u3005"},"content":{"rendered":"<p>\u9759\u7684\u30d5\u30a1\u30a4\u30eb\u914d\u4fe1\u3002json\u30d1\u30fc\u30b9<\/p>\n<pre class=\"lang:go decode:true  \">package main\r\n\r\nimport (\r\n\t\"encoding\/json\"\r\n\r\n\t\"io\/ioutil\"\r\n\t\"log\"\r\n\t\"net\/http\"\r\n\t\"os\"\r\n\t\"path\/filepath\"\r\n)\r\n\r\nfunc main() {\r\n\r\n\t\/\/\u9759\u7684\u30d5\u30a1\u30a4\u30eb\u914d\u4fe1\r\n\r\n\t\/\/http.Dir\u306fhttp:\/\/foo:bar\/\u306e\u3088\u3046\u306a\u30af\u30a8\u30ea\u306e\u306a\u3044URL\u304c\u30ed\u30fc\u30ab\u30eb\u306e\u3069\u306e\u30d1\u30b9(root)\u3068\u306a\u308b\u304b\u3068\u3044\u3046\u610f\u5473\r\n\t\/\/http:\/\/foo:bar\/aaa\u306e\u3088\u3046\u306a\u6307\u5b9a\u306e\u5834\u5408\/aaa\u3082\u30ed\u30fc\u30ab\u30eb\u30d1\u30b9\u306b\u5fc5\u8981\u3068\u306a\u308b\u3002\r\n\r\n\tcurrentPath, _ := os.Executable()\r\n\tlocalRoot := filepath.Dir(currentPath)\r\n\tremoteRoot := http.Dir(localRoot)\r\n\r\n\thttp.Handle(\"\/\", http.FileServer(remoteRoot))\r\n\r\n\t\/\/\u30d1\u30b9\u3092\u5909\u66f4\u3059\u308b\u5834\u5408\r\n\t\/\/http.Handle(\"\/html\/\", http.StripPrefix(\"\/html\/\", http.FileServer(remoteRoot)))\r\n\r\n\t\/\/\u30ea\u30af\u30a8\u30b9\u30c8\u51e6\u7406\r\n\r\n\thttp.HandleFunc(\"\/read\", read)\r\n\r\n\tif err := http.ListenAndServe(\":9000\", nil); err != nil {\r\n\t\tlog.Fatal(err)\r\n\t}\r\n}\r\n\r\nvar dt interface{}\r\n\r\n\/*\r\n\r\ntype DataFormat struct {\r\n\tRequestID string\r\n}\r\n\r\n\u306e\u3088\u3046\u306b\u69cb\u9020\u4f53\u3092\u5ba3\u8a00\u3057\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067json.Unmarshal\u3092\u53d7\u3051\u308b\u3002\r\n\r\ndt := DataFormat{}\r\n\r\n*\/\r\n\r\nfunc read(w http.ResponseWriter, r *http.Request) {\r\n\r\n\tif r.Method != http.MethodPost {\r\n\t\tw.WriteHeader(http.StatusServiceUnavailable)\r\n\t\treturn\r\n\t}\r\n\r\n\t\/*\r\n\r\n\t\tjson.Unmarshal(data []byte, v interface{}) error\r\n\t\t\u3067json\u3092\u53d7\u3051\u53d6\u308b\r\n\r\n\t\tio.Reader\u304b\u3089[]byte\u306b\u5909\u63db\r\n\r\n\t\tioutil.ReadAll(io.Reader) ([]bytes, error)\r\n\t\tbytes.Buffer.ReadFrom(io.Reader) (int64, error)\r\n\t\tio.Copy(io.Writer, io.Reader) (int64, error)\r\n\r\n\t*\/\r\n\r\n\tbody, err := ioutil.ReadAll(r.Body)\r\n\tif err != nil {\r\n\t\tw.WriteHeader(http.StatusServiceUnavailable)\r\n\t\tlog.Print(err)\r\n\t\treturn\r\n\t}\r\n\r\n\tif err := json.Unmarshal(body, &amp;dt); err != nil {\r\n\t\tw.WriteHeader(http.StatusServiceUnavailable)\r\n\t\tlog.Print(err)\r\n\t\treturn\r\n\t}\r\n\r\n\t\/\/dt\u306finterface\u578b\u3067\u5b9f\u969b\u306fmap\u578b\r\n\r\n\t\/*\r\n\r\n\t\tjson\u304c\u30cd\u30b9\u30c8\u3057\u3066\u3044\u306a\u3044\u5834\u5408\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\u306e\u307f\u3067\u53d6\u308a\u51fa\u305b\u308b\u3002\r\n\r\n\t\td := dt.(map[string]interface{})\r\n\t\tlog.Print(d[\"main\"])\r\n\t\tlog.Print(reflect.TypeOf(d)) -&gt; map[string]interface {}\r\n\r\n\t\t\u5168\u4f53\u304c\u914d\u5217\u306ejson\u306a\u3089\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u53d6\u308a\u51fa\u305b\u308b\r\n\t\t\u914d\u5217\u306f[]interface{}\u306erange\u3067\u51e6\u7406\r\n\r\n\t\tfor _, aa := range dt.([]interface{}) {\r\n\t\t\tfor cc, bb := range aa.(map[string]interface{}) {\r\n\t\t\t\tlog.Print(cc)\r\n\t\t\t\tlog.Print(bb)\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\u4e2d\u3067\u914d\u5217\u30cd\u30b9\u30c8\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306a\u5834\u5408\r\n\r\n\t\tfor k, v := range dt.(map[string]interface{}) {\r\n\r\n\t\t\tlog.Print(\"1:\", k) \/\/json\u306ekey\u304c\u5165\u308b\r\n\t\t\tlog.Print(\"2:\", v) \/\/json\u306evalue\u304c\u5165\u308b\r\n\r\n\t\t\t\/\/v\u3068vv\u306f\u540c\u3058\u5024\u3067\u578b\u306etypeOf\u304c\u5165\u308b\u306e\u3067\u306f\u306a\u3044\r\n\t\t\tswitch vv := v.(type) {\r\n\t\t\tcase []interface{}:\r\n\t\t\t\tfor _, y := range vv {\r\n\t\t\t\t\tyy := y.(map[string]interface{})\r\n\t\t\t\t\tlog.Print(\"3:\", yy[\"ID\"])\r\n\t\t\t\t\tlog.Print(\"4:\", yy[\"\u540d\u79f0\"])\r\n\t\t\t\t}\r\n\t\t\tdefault:\r\n\t\t\t\tlog.Print(\"5:\", v)\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t*\/\r\n\r\n\tw.Header().Set(\"content-type\", \"application\/json;charset=UTF-8\")\r\n\tw.WriteHeader(http.StatusOK)\r\n\r\n\tif err := json.NewEncoder(w).Encode(dt); err != nil {\r\n\t\tw.WriteHeader(http.StatusServiceUnavailable)\r\n\t\tlog.Print(err)\r\n\t\treturn\r\n\t}\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9759\u7684\u30d5\u30a1\u30a4\u30eb\u914d\u4fe1\u3002json\u30d1\u30fc\u30b9 package main import ( &#8220;encoding\/json&#8221; &#8220;io\/ioutil&#8221; &#8220;log&#8221; &#8220;net\/http&#8221; &#8220;os&#8221; &#8220;path\/filepath&#8221; ) fun &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2453\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;golang\u8272\u3005&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[148],"class_list":["post-2453","post","type-post","status-publish","format-standard","hentry","category-1","tag-golang"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2453"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2453\/revisions"}],"predecessor-version":[{"id":2454,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2453\/revisions\/2454"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}