{"id":207715,"date":"2025-07-08T15:03:04","date_gmt":"2025-07-08T07:03:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207715\/"},"modified":"2025-07-08T15:03:04","modified_gmt":"2025-07-08T07:03:04","slug":"gin-%e7%bb%93%e6%9e%84%e5%8f%82%e6%95%b0%e8%a2%ab%e9%aa%8c%e8%af%81%e4%b8%ba-null","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207715\/","title":{"rendered":"Gin &#8211; \u7ed3\u6784\u53c2\u6570\u88ab\u9a8c\u8bc1\u4e3a null"},"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>Gin &#8211; \u7ed3\u6784\u53c2\u6570\u88ab\u9a8c\u8bc1\u4e3a null<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 22:00:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGin &#8211; \u7ed3\u6784\u53c2\u6570\u88ab\u9a8c\u8bc1\u4e3a null\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u8fd9\u91cc\u6709\u4e00\u4e2a\u51fd\u6570\u6765\u521b\u5efa post \u8bf7\u6c42\u5e76\u5c06\u7ed3\u6784\u7c7b\u578b\u7684\u65b0\u7528\u6237\u6dfb\u52a0\u5230\u5207\u7247\u4e2d\uff08api \u7684\u6570\u636e\u4ec5\u5728\u5185\u5b58\u4e2d\u8fd0\u884c\uff0c\u56e0\u6b64\u6ca1\u6709\u6570\u636e\u5e93\uff09\uff1a<\/p>\n<pre>type user struct {\n    id        string `json:\"id\"`\n    firstname string `json:\"first_name\"`\n    lastname  string `json:\"last_name\"`\n    email     string `json:\"email\"`\n}\n\nvar users = []user{\n    {id: \"1\", firstname: \"john\", lastname: \"doe\", email: \"[email&nbsp;protected]\"},\n}\n\nfunc createuser(c *gin.context) {\n\n    var newuser user\n\n    if len(newuser.id) == 0 {\n        c.json(http.statusbadrequest, gin.h{\"message\": \"user id is null\"})\n        return\n    }\n\n    users = append(users, newuser)\n    c.json(http.statuscreated, newuser)\n}<\/pre>\n<p>\u4e00\u5207\u5de5\u4f5c\u6b63\u5e38\uff0c\u76f4\u5230\u6211\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u201cif \u8bed\u53e5\u201d\u6765\u68c0\u67e5\u901a\u8fc7 post \u8bf7\u6c42\u53d1\u9001\u7684\u7528\u6237 id \u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<p>\u95ee\u9898\u662f if \u8bed\u53e5\u5e94\u8be5\u8fd4\u56de false\uff0c\u4f46\u5b83\u5374\u8fd4\u56de true\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6211\u5c1d\u8bd5\u4f7f\u7528\u4ee5\u4e0b\u6570\u636e\u53d1\u51fa\u53d1\u5e03\u8bf7\u6c42\uff1a<\/p>\n<pre>{\n    \"id\": \"2\",\n    \"first_name\": \"jane\",\n    \"last_name\": \"doe\",\n    \"email\": \"[email&nbsp;protected]\"\n}<\/pre>\n<p>\u68c0\u67e5 id \u957f\u5ea6\u4e3a 0 \u7684 if \u8bed\u53e5\u5c06\u4e3a true\uff0c\u56e0\u6b64\u8fd4\u56de statusbadrequest\u3002\u5982\u679c\u4e5f\u5c1d\u8bd5\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff1a<\/p>\n<pre>if newUser.ID == \"\" {\n}<\/pre>\n<p>\u4f46\u662f\u5f53\u4e0d\u5e94\u8be5\u8fd4\u56de true \u65f6\uff0c\u8fd9\u4e5f\u4f1a\u8fd4\u56de true\u3002<\/p>\n<p>\u5982\u679c\u6211\u5220\u9664\u6b64\u68c0\u67e5\u5e76\u521b\u5efa post \u8bf7\u6c42\uff0c\u5b83\u5c31\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u4e14\u5f53\u6211\u53d1\u51fa\u65b0\u7684 get \u8bf7\u6c42\u65f6\uff0c\u5c06\u663e\u793a\u65b0\u6dfb\u52a0\u7684\u6570\u636e\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8fd9\u4e9b if \u8bed\u53e5\u8fd4\u56de true\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u60a8\u4f7f\u7528 <code>var newuser user<\/code> \u8bed\u53e5\u521b\u5efa\u65b0\u7528\u6237\u5bf9\u8c61\u65f6\uff0c\u60a8\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u7a7a\u7528\u6237\u5bf9\u8c61\u3002\u60a8\u4ecd\u7136\u5fc5\u987b\u5c06\u8981\u53d1\u9001\u7684 json \u5b57\u7b26\u4e32\u7ed1\u5b9a\u5230\u8be5\u5bf9\u8c61\u4e2d\u3002\u4e3a\u6b64\uff0c\u60a8\u9700\u8981\u505a\u7684\u662f\uff1a<code>c.bindjson(&amp;newuser)<\/code>\u3002\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>func createUser(c *gin.Context) {\n\n    var newUser user\n    err := c.BindJSON(&amp;newUser)\n    if err != nil {\n        c.JSON(http.StatusBadRequest, gin.H{\"error\": err.Error()})\n    }\n\n    if len(newUser.ID) == 0 {\n        c.JSON(http.StatusBadRequest, gin.H{\"message\": \"user id is null\"})\n        return\n    }\n\n    users = append(users, newUser)\n    c.JSON(http.StatusCreated, newUser)\n}\n<\/pre>\n<p>\u60a8\u53ef\u4ee5\u67e5\u770b\u6b64\u94fe\u63a5\u4ee5\u83b7\u53d6 gin \u63d0\u4f9b\u7684\u793a\u4f8b\uff1a<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aGin &#8211; \u7ed3\u6784\u53c2\u6570\u88ab\u9a8c\u8bc1\u4e3a null\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\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-207715","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207715","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=207715"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207715\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}