{"id":207605,"date":"2025-07-08T08:08:36","date_gmt":"2025-07-08T00:08:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207605\/"},"modified":"2025-07-08T08:08:36","modified_gmt":"2025-07-08T00:08:36","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e7%bb%93%e6%9e%84%e4%bd%93%e4%bd%9c%e4%b8%ba%e5%8f%82%e6%95%b0%e4%bc%a0%e9%80%92%e7%bb%99%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207605\/","title":{"rendered":"\u5982\u4f55\u5c06\u7ed3\u6784\u4f53\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u5c06\u7ed3\u6784\u4f53\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 22:03:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06\u7ed3\u6784\u4f53\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u600e\u6837\u624d\u80fd\u505a\u8fd9\u6837\u7684\u4e8b\u60c5\uff1f<\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5c06 <code>struct<\/code> \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 go \u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<pre>func handleEntityProperties(w http.ResponseWriter, r *http.Request) {\n    const sliceSize = 100\n    var entityProperties struct {\n        Instance string `json:\"instance\"`\n        Entities []struct {\n            Id         string            `json:\"id\"`\n            Properties map[string]string `json:\"properties\"`\n            Type       string            `json:\"type\"`\n        } `json:\"entities\"`\n    }\n\n    body, err := ioutil.ReadAll(r.Body)\n    if err != nil {\n        panic(err)\n    }\n\n    if !json.Valid([]byte(body)) {\n        fmt.Fprintf(w, \"invalid json\")\n        return\n    }\n\n    err = json.Unmarshal(body, &amp;entityProperties)\n\n    sendData(entityProperties.Entities[0:100])\n\n    return\n}\n\nfunc sendData(entities struct) {\n    log.Println(\"Doing things with entities \", entities)\n}<\/pre>\n<p>\u6b63\u5982\u60a8\u5728\u4ee3\u7801\u4e2d\u770b\u5230\u7684\uff0c\u6211\u6b63\u5728\u5c1d\u8bd5\u5c06 <code>entityproperties.entities<\/code> <code>struct<\/code> \u7684\u524d 100 \u4e2a\u5143\u7d20\u53d1\u9001\u5230 <code>senddata<\/code>\u3002\u6211\u77e5\u9053\u8fd9\u5728\u8bed\u6cd5\u4e0a\u662f\u9519\u8bef\u7684\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u53ea\u9700\u5728\u51fd\u6570\u4e4b\u5916\u58f0\u660e\u60a8\u7684\u7c7b\u578b\uff1a<\/p>\n<pre>type entity struct {\n    id         string            `json:\"id\"`\n    properties map[string]string `json:\"properties\"`\n    type       string            `json:\"type\"`\n}<\/pre>\n<p>\u5e76\u5728 <code>handleentityproperties()<\/code> \u548c <code>senddata()<\/code> \u7684\u7b7e\u540d\u4e2d\u91cd\u7528\u5b83\uff1a<\/p>\n<pre>func handleentityproperties(w http.responsewriter, r *http.request) {\n    const slicesize = 100\n    var entityproperties struct {\n        instance string   `json:\"instance\"`\n        entities []entity `json:\"entities\"`\n    }\n\n    body, err := ioutil.readall(r.body)\n    if err != nil {\n        panic(err)\n    }\n\n    if !json.valid([]byte(body)) {\n        fmt.fprintf(w, \"invalid json\")\n        return\n    }\n\n    err = json.unmarshal(body, &amp;entityproperties)\n\n    senddata(entityproperties.entities[0:slicesize])\n\n    return\n}\n\nfunc senddata(entities []entity) {\n    log.println(\"doing things with entities \", entities)\n}<\/pre>\n<p>\u53e6\u8bf7\u6ce8\u610f\uff0c\u4e0d\u80fd\u4fdd\u8bc1\u5ba2\u6237\u7aef\u5c06\u53d1\u9001\u81f3\u5c11 100 \u4e2a\u5b9e\u4f53\uff0c\u56e0\u6b64\u60a8\u5e94\u8be5\u68c0\u67e5\u5207\u7247\u8868\u8fbe\u5f0f\u662f\u5426\u53ef\u80fd\u5bfc\u81f4\u8fd0\u884c\u65f6\u6050\u614c\uff1a<\/p>\n<pre>max := 100\nif len(entityproperties.entities) &lt; max {\n    max = len(entityproperties.entities)\n}\nsenddata(entityproperties.entities[:max])<\/pre>\n<p>\u6b64\u5916\uff0c\u68c0\u67e5\u65e0\u6548 json \u662f\u4e0d\u5fc5\u8981\u7684\uff1a\u5982\u679c json \u65e0\u6548\uff0c \u5c06\u62a5\u544a\uff08\u975e <code>nil<\/code>\uff09\u9519\u8bef\uff0c\u60a8\u5c31\u4f1a\u77e5\u9053\u3002<\/p>\n<p>\u66f4\u8fdb\u4e00\u6b65\uff0c\u60a8\u751a\u81f3\u4e0d\u5fc5\u5c06\u5b8c\u6574\u7684\u6b63\u6587\u8bfb\u5165\u5185\u5b58\uff08\u8bfb\u5165\u5b57\u8282\u7247\uff09\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 \u76f4\u63a5\u4ece\u4e2d\u8bfb\u53d6\uff08\u65e0\u9700\u4e2d\u95f4\u5185\u5b58\u7f13\u51b2\u533a\uff09\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>dec := json.newdecoder(r.body)\nif err := dec.decode(&amp;entityproperties); err != nil {\n    \/\/ handle error\n}<\/pre>\n<p>\u6700\u540e\u7684 <code>return<\/code> \u8bed\u53e5\u4e5f\u662f\u4e0d\u5fc5\u8981\u7684\u3002<\/p>\n<p>\u56e0\u6b64\u6539\u8fdb\u7248\u672c\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func handleEntityProperties(w http.ResponseWriter, r *http.Request) {\n    var entityProperties struct {\n        Instance string   `json:\"instance\"`\n        Entities []entity `json:\"entities\"`\n    }\n\n    dec := json.NewDecoder(r.Body)\n    if err := dec.Decode(&amp;entityProperties); err != nil {\n        \/\/ handle error\n        http.Error(w, \"invalid json\", http.StatusBadRequest)\n        return\n    }\n\n    max := 100\n    if len(entityProperties.Entities) &lt; max {\n        max = len(entityProperties.Entities)\n    }\n    sendData(entityProperties.Entities[:max])\n}<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5c06\u7ed3\u6784\u4f53\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207605","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207605"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207605\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}