{"id":6846,"date":"2024-11-14T13:13:19","date_gmt":"2024-11-14T05:13:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6846\/"},"modified":"2024-11-14T13:13:19","modified_gmt":"2024-11-14T05:13:19","slug":"go-mysql-gin-%e6%8a%a5%e9%94%99%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e6%97%a0%e6%95%88%e5%86%85%e5%ad%98%e5%9c%b0%e5%9d%80%e6%88%96%e7%a9%ba%e6%8c%87%e9%92%88%e5%8f%96%e6%b6%88","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6846\/","title":{"rendered":"Go MySQL Gin \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201c\u65e0\u6548\u5185\u5b58\u5730\u5740\u6216\u7a7a\u6307\u9488\u53d6\u6d88\u5f15\u7528\u201d\u95ee\u9898\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172999351181836.jpg\" class=\"aligncenter\" title=\"Go MySQL Gin \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201c\u65e0\u6548\u5185\u5b58\u5730\u5740\u6216\u7a7a\u6307\u9488\u53d6\u6d88\u5f15\u7528\u201d\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"Go MySQL Gin \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201c\u65e0\u6548\u5185\u5b58\u5730\u5740\u6216\u7a7a\u6307\u9488\u53d6\u6d88\u5f15\u7528\u201d\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>go  gin \u62a5\u9519\u5206\u6790<\/strong><\/p>\n<p>\u6570\u636e\u5e93\u4ea4\u4e92\u65f6\u53d1\u751f panic \u9519\u8bef\uff0c\u63d0\u793a\u201c\u65e0\u6548\u5185\u5b58\u5730\u5740\u6216\u7a7a\u6307\u9488\u53d6\u6d88\u5f15\u7528\u201d\u3002\u4ee5\u4e0b\u662f\u5bf9\u95ee\u9898\u6839\u6e90\u7684\u5206\u6790\u548c\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>\u9519\u8bef\u539f\u56e0\u53ef\u80fd\u662f\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u4e86\u5df2\u5173\u95ed\u7684 sql \u8bed\u53e5 (stmt)\u3002<\/li>\n<li>\u672a\u6b63\u786e\u5904\u7406 stmt \u7684\u751f\u547d\u5468\u671f\uff0c\u5bfc\u81f4\u5728 stmt \u5173\u95ed\u540e\u4ecd\u88ab\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><strong>\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<p>\u786e\u4fdd\u5728\u4e0d\u518d\u4f7f\u7528 stmt \u65f6\u5c06\u5176\u5173\u95ed\u3002\u53ef\u4ee5\u4f7f\u7528 defer \u5173\u952e\u5b57\u6765\u786e\u4fdd\u5728\u51fd\u6570\u8fd4\u56de\u65f6\u81ea\u52a8\u5173\u95ed stmt\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>stmt, err := db.Prepare(\"SELECT * FROM users WHERE id = ?\")\nif err != nil {\n    return err\n}\ndefer stmt.Close()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4f7f\u7528 stmt.exec \u65b9\u6cd5\u65f6\uff0c\u8bf7\u786e\u4fdd\u4f20\u9012\u6b63\u786e\u7684\u53c2\u6570\u7c7b\u578b\u548c\u6570\u91cf\u3002\u6b64\u5916\uff0c\u8bf7\u8003\u8651\u4f7f\u7528 stmt.execcontext \u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u4e0a\u4e0b\u6587\u53d6\u6d88\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u53d6\u6d88\u67e5\u8be2\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGo MySQL Gin \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201c\u65e0\u6548\u5185\u5b58\u5730\u5740\u6216\u7a7a\u6307\u9488\u53d6\u6d88\u5f15\u7528\u201d\u95ee\u9898\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go gin \u62a5\u9519\u5206\u6790 \u6570\u636e\u5e93\u4ea4\u4e92\u65f6\u53d1\u751f panic \u9519\u8bef\uff0c\u63d0\u793a\u201c\u65e0\u6548\u5185\u5b58\u5730\u5740\u6216\u7a7a\u6307\u9488\u53d6\u6d88\u5f15\u7528\u201d\u3002\u4ee5\u4e0b\u662f\u5bf9\u95ee\u9898\u6839\u6e90\u7684\u5206\u6790\u548c\u89e3\u51b3\u65b9\u6848\uff1a \u9519\u8bef\u539f\u56e0\u53ef\u80fd\u662f\uff1a \u4f7f\u7528\u4e86\u5df2\u5173\u95ed\u7684 sql \u8bed\u53e5 (stmt)\u3002 \u672a\u6b63\u786e\u5904\u7406 stmt \u7684\u751f\u547d\u5468\u671f\uff0c\u5bfc\u81f4\u5728 stmt \u5173\u95ed\u540e\u4ecd\u88ab\u4f7f\u7528\u3002 \u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff1a \u786e\u4fdd\u5728\u4e0d\u518d\u4f7f\u7528 stmt \u65f6\u5c06\u5176\u5173\u95ed\u3002\u53ef\u4ee5\u4f7f\u7528 defer \u5173\u952e\u5b57\u6765\u786e\u4fdd\u5728\u51fd\u6570\u8fd4\u56de\u65f6\u81ea\u52a8\u5173\u95ed stmt\uff0c\u5982\u4e0b\u6240\u793a\uff1a stmt, err := db.Prepare(&#8220;SELECT * FROM users WHERE id = ?&#8221;) if err != nil { return err } defer stmt.Close() \u767b\u5f55\u540e\u590d\u5236 \u5728\u4f7f\u7528 stmt.exec \u65b9\u6cd5\u65f6\uff0c\u8bf7\u786e\u4fdd\u4f20\u9012\u6b63\u786e\u7684\u53c2\u6570\u7c7b\u578b\u548c\u6570\u91cf\u3002\u6b64\u5916\uff0c\u8bf7\u8003\u8651\u4f7f\u7528 stmt.execcontext \u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u4e0a\u4e0b\u6587\u53d6\u6d88\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u53d6\u6d88\u67e5\u8be2\u3002 \u4ee5\u4e0a\u5c31\u662fGo MySQL Gin \u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201c\u65e0\u6548\u5185\u5b58\u5730\u5740\u6216\u7a7a\u6307\u9488\u53d6\u6d88\u5f15\u7528\u201d\u95ee\u9898\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6846","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6846"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6846\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}