{"id":207592,"date":"2025-07-08T14:15:30","date_gmt":"2025-07-08T06:15:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207592\/"},"modified":"2025-07-08T14:15:30","modified_gmt":"2025-07-08T06:15:30","slug":"%e5%b0%86%e4%b8%bb%e4%bd%93%e8%a7%a3%e7%a0%81%e4%b8%ba%e7%bb%93%e6%9e%84%e6%97%b6%ef%bc%8cgolang-%e4%b8%8d%e4%bc%9a%e4%ba%a7%e7%94%9f%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207592\/","title":{"rendered":"\u5c06\u201c{}\u201d\u4e3b\u4f53\u89e3\u7801\u4e3a\u7ed3\u6784\u65f6\uff0cGolang \u4e0d\u4f1a\u4ea7\u751f\u9519\u8bef"},"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>\u5c06\u201c{}\u201d\u4e3b\u4f53\u89e3\u7801\u4e3a\u7ed3\u6784\u65f6\uff0cGolang \u4e0d\u4f1a\u4ea7\u751f\u9519\u8bef<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 20:27:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662fGolang\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5c06\u201c{}\u201d\u4e3b\u4f53\u89e3\u7801\u4e3a\u7ed3\u6784\u65f6\uff0cGolang \u4e0d\u4f1a\u4ea7\u751f\u9519\u8bef\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728rest api\u4e2d\uff0c\u5f53body\u8bbe\u7f6e\u4e3a\u201c{}\u201d\u65f6\uff0cjson\u89e3\u7801\u5668\u4e0d\u4f1a\u751f\u6210\u9519\u8bef\u3002\u8fd9\u4f7f\u5f97\u6709\u5fc5\u8981\u68c0\u67e5\u76ee\u6807\u7ed3\u6784\u662f\u5426\u4ecd\u7136\u662f <code>nil<\/code>\u3002<\/p>\n<p>\u6211\u9700\u8981\u68c0\u67e5\u8be5\u5e93\u662f\u5426\u5e94\u8be5\u50cf\u8fd9\u6837\u5de5\u4f5c\uff0c\u6216\u8005\u8fd9\u662f\u5426\u662f\u4e00\u4e2a\u95ee\u9898\u3002<\/p>\n<pre>\/\/ Client Side this request\nreq, err := http.NewRequest(\"POST\", \"url\", strings.NewReader(\"{}\") )\n\n\/\/ Curl equivalent: \ncurl -X POST -d '{}' http:\/\/api:8080\/r\/primitives\/multiply\n\n\/\/ Server side\ntype Operands struct {\n    Values []float64 `json:\"Values\"`\n}\n\nfunc handler(req *http.Request, res *http.Response) (string, error) {\n    operands := Operands{}\n    err := json.NewDecoder(req.Body).Decode(&amp;operands)\n    if err != nil {\n        res.StatusCode = 400\n        res.Status = http.StatusText(res.StatusCode)\n        http.StatusText(res.StatusCode)\n        return \"\", err\n    }\n     operands.Values[0] \/\/ It will fail here.\n}<\/pre>\n<p>\u7f16\u8f91 1\uff1a\u89e3\u7801\u5668\u53ef\u4ee5\u5728\u751f\u6210\u9519\u8bef\u7684\u7a7a\u4e3b\u4f53\u201c\u201d\u4e0b\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u6b63\u786e\u7684\u4e3b\u4f53\u4e0b\u6b63\u5e38\u5de5\u4f5c\uff0c\u5982\u4e0b\u6240\u793a\uff1a <code>{\"values\" : [ 5.0 , 2.0 ]}<\/code> \u7f16\u8f912\uff1a\u8fd9\u91cc\u7684\u95ee\u9898\u662f\uff0c\u4f7f\u7528\u201c{}\u201d\u4e3b\u4f53\uff0c\u89e3\u7801\u65f6\u4e0d\u4f1a\u8fd4\u56de\u9519\u8bef\uff0c\u800c\u662f\u5c06\u76ee\u6807\u7ed3\u6784\u4fdd\u6301\u4e3anil\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><code>{}<\/code> \u53ea\u662f\u4e00\u4e2a\u7a7a\u7684 json \u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u5f88\u597d\u5730\u89e3\u7801\u4e3a <code>operands<\/code> \u7ed3\u6784\u4f53\uff0c\u56e0\u4e3a\u8be5\u7ed3\u6784\u4f53\u4e0d\u9700\u8981\u5728 <code>operands<\/code> \u6570\u7ec4\u4e2d\u5305\u542b\u4efb\u4f55\u5185\u5bb9\u3002<\/p>\n<p>\u60a8\u9700\u8981\u81ea\u5df1\u9a8c\u8bc1\u8fd9\u4e00\u70b9\uff0c\u4f8b\u5982<\/p>\n<pre>err := json.NewDecoder(req.Body).Decode(&amp;operands)\nif err != nil || len(operands.Values) == 0{<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5c06\u201c{}\u201d\u4e3b\u4f53\u89e3\u7801\u4e3a\u7ed3\u6784\u65f6\uff0cGolang \u4e0d\u4f1a\u4ea7\u751f\u9519\u8bef\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\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-207592","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207592","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=207592"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207592\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}