{"id":900,"date":"2024-11-07T12:49:48","date_gmt":"2024-11-07T04:49:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/900\/"},"modified":"2024-11-07T12:49:48","modified_gmt":"2024-11-07T04:49:48","slug":"go-%e4%b8%ad%e4%bd%bf%e7%94%a8-mysql-%e5%92%8c-gin-%e6%97%b6%ef%bc%8c%e9%81%87%e5%88%b0panic-recoveredruntime-error-invalid-memory-address-or-nil-pointer-dereference%e9%94%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/900\/","title":{"rendered":"Go \u4e2d\u4f7f\u7528 MySQL \u548c Gin \u65f6\uff0c\u9047\u5230\u201cpanic recovered:runtime error: invalid memory address or nil pointer dereference\u201d\u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1004\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173054672044151.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173054672044151.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173054672044151-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173054672044151-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173054672044151-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Go \u4e2d\u4f7f\u7528 MySQL \u548c Gin \u65f6\uff0c\u9047\u5230\u201cpanic recovered:runtime error: invalid memory address or nil pointer dereference\u201d\u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"Go \u4e2d\u4f7f\u7528 MySQL \u548c Gin \u65f6\uff0c\u9047\u5230\u201cpanic recovered:runtime error: invalid memory address or nil pointer dereference\u201d\u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>go \u4e2d mysql \u548c gin \u64cd\u4f5c\u62a5\u9519<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 golang \u7684 mysql \u9a71\u52a8\u548c gin \u6846\u67b6\u65f6\u9047\u5230\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>panic recovered:\nruntime error: invalid memory address or nil pointer dereference\n\/snap\/go\/8489\/src\/database\/sql\/sql.go:2578 (0x4f30c2)\n        (*stmt).execcontext: s.closemu.rlock()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong><\/p>\n<p>\u8be5\u9519\u8bef\u8868\u660e\u5c1d\u8bd5\u5728\u5173\u95ed\u7684\u8bed\u53e5\u4e0a\u6267\u884c\u64cd\u4f5c\u3002\u5f88\u53ef\u80fd\u662f\u56e0\u4e3a\u4f7f\u7528\u4e86 mysql \u9a71\u52a8\u7684 stmt.exec() \u800c\u6ca1\u6709\u4f7f\u7528 stmt.execcontext()\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u786e\u4fdd\u59cb\u7ec8\u4f7f\u7528 stmt.execcontext()\uff0c\u5373\u4f7f\u672a\u4f7f\u7528\u4e0a\u4e0b\u6587\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>_, err := stmt.ExecContext(context.TODO(), req.IdCode, c.Request.RemoteAddr)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGo \u4e2d\u4f7f\u7528 MySQL \u548c Gin \u65f6\uff0c\u9047\u5230\u201cpanic recovered:runtime error: invalid memory address or nil pointer dereference\u201d\u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go \u4e2d mysql \u548c gin \u64cd\u4f5c\u62a5\u9519 \u95ee\u9898\uff1a \u4f7f\u7528 golang \u7684 mysql \u9a71\u52a8\u548c gin \u6846\u67b6\u65f6\u9047\u5230\u5982\u4e0b\u9519\u8bef\uff1a panic recovered: runtime error: invalid memory address or nil pointer dereference \/snap\/go\/8489\/src\/database\/sql\/sql.go:2578 (0x4f30c2) (*stmt).execcontext: s.closemu.rlock() \u767b\u5f55\u540e\u590d\u5236 \u539f\u56e0\uff1a \u8be5\u9519\u8bef\u8868\u660e\u5c1d\u8bd5\u5728\u5173\u95ed\u7684\u8bed\u53e5\u4e0a\u6267\u884c\u64cd\u4f5c\u3002\u5f88\u53ef\u80fd\u662f\u56e0\u4e3a\u4f7f\u7528\u4e86 mysql \u9a71\u52a8\u7684 stmt.exec() \u800c\u6ca1\u6709\u4f7f\u7528 stmt.execcontext()\u3002 \u89e3\u51b3\u65b9\u6cd5\uff1a \u786e\u4fdd\u59cb\u7ec8\u4f7f\u7528 stmt.execcontext()\uff0c\u5373\u4f7f\u672a\u4f7f\u7528\u4e0a\u4e0b\u6587\u53c2\u6570\u3002\u4f8b\u5982\uff1a _, err := stmt.ExecContext(context.TODO(), req.IdCode, c.Request.RemoteAddr) \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662fGo \u4e2d\u4f7f\u7528 MySQL \u548c Gin \u65f6\uff0c\u9047\u5230\u201cpanic recovered:runtime error: [&hellip;]<\/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-900","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/900","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=900"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/900\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}