{"id":6862,"date":"2024-11-14T08:03:44","date_gmt":"2024-11-14T00:03:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6862\/"},"modified":"2024-11-14T08:03:44","modified_gmt":"2024-11-14T00:03:44","slug":"go-mysql-gin-%e6%8a%a5%e9%94%99-invalid-memory-address-or-nil-pointer-dereference-%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6862\/","title":{"rendered":"Go + MySQL + Gin \u62a5\u9519 &#8220;invalid memory address or nil pointer dereference&#8221; \u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172999944335369.jpg\" class=\"aligncenter\" title=\"Go + MySQL + Gin \u62a5\u9519 &#8220;invalid memory address or nil pointer dereference&#8221; \u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"Go + MySQL + Gin \u62a5\u9519 &#8220;invalid memory address or nil pointer dereference&#8221; \u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>Go + MySQL + Gin \u62a5\u9519\u5206\u6790<\/strong><\/p>\n<p>\u4f60\u9047\u5230\u7684\u9519\u8bef\u662f &#8220;runtime error: invalid memory address or nil pointer dereference\u3002&#8221;\uff0c\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u5728\u6307\u9488\u4e3a nil \u6216\u6307\u5411\u65e0\u6548\u5185\u5b58\u5730\u5740\u65f6\u4f7f\u7528\u4e86\u5b83\u800c\u5f15\u8d77\u7684\u3002<\/p>\n<p>\u6839\u636e\u4f60\u63d0\u4f9b\u7684\u9519\u8bef\u5806\u6808\uff0c\u53ef\u4ee5\u770b\u51fa\u95ee\u9898\u51fa\u5728\u4f7f\u7528 stmt.Exec \u65b9\u6cd5\u5904\u3002<\/p>\n<p>\u53ef\u80fd\u7684\u9519\u8bef\u539f\u56e0\uff1a<\/p>\n<ul>\n<li>\u8c03\u7528 stmt.Close \u65b9\u6cd5\u5173\u95ed\u4e86\u51c6\u5907\u597d\u7684\u8bed\u53e5\uff0c\u5bfc\u81f4\u5176\u6307\u9488\u4e3a nil\u3002<\/li>\n<li>\u51c6\u5907\u8bed\u53e5\u5728\u4f7f\u7528 stmt.Exec \u4e4b\u524d\u6ca1\u6709\u6b63\u786e\u5730\u91cd\u65b0\u51c6\u5907\u3002<\/li>\n<\/ul>\n<p>Stmt.Exec \u65b9\u6cd5\u7528\u4e8e\u6267\u884c\u5df2\u51c6\u5907\u7684 SQL \u8bed\u53e5\uff0c\u800c Stmt.ExecContext \u65b9\u6cd5\u63a5\u53d7 context.Context \u53c2\u6570\uff0c\u5141\u8bb8\u5728\u6267\u884c\u65f6\u4f20\u9012\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f MySQL \u9a71\u52a8\u4e2d\u7684 stmt.Exec\uff0c\u5e76\u4e14\u6ca1\u6709\u4f7f\u7528 stmt.ExecContext\uff0c\u5219\u9700\u8981\u786e\u4fdd\u5728\u6bcf\u6b21\u4f7f\u7528 stmt.Exec \u4e4b\u524d\u91cd\u65b0\u51c6\u5907\u8be5\u8bed\u53e5\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8bf7\u5c1d\u8bd5\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u5728\u4f7f\u7528 stmt.Exec \u4e4b\u524d\u91cd\u65b0\u51c6\u5907\u8be5\u8bed\u53e5\u3002<\/li>\n<li>\u786e\u4fdd\u5728\u4e0d\u518d\u9700\u8981\u51c6\u5907\u597d\u7684\u8bed\u53e5\u65f6\u5c06\u5176\u5173\u95ed\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662fGo + MySQL + Gin \u62a5\u9519 &#8220;invalid memory address or nil pointer dereference&#8221; \u5982\u4f55\u89e3\u51b3\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 + MySQL + Gin \u62a5\u9519\u5206\u6790 \u4f60\u9047\u5230\u7684\u9519\u8bef\u662f &#8220;runtime error: invalid memory address or nil pointer dereference\u3002&#8221;\uff0c\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u5728\u6307\u9488\u4e3a nil \u6216\u6307\u5411\u65e0\u6548\u5185\u5b58\u5730\u5740\u65f6\u4f7f\u7528\u4e86\u5b83\u800c\u5f15\u8d77\u7684\u3002 \u6839\u636e\u4f60\u63d0\u4f9b\u7684\u9519\u8bef\u5806\u6808\uff0c\u53ef\u4ee5\u770b\u51fa\u95ee\u9898\u51fa\u5728\u4f7f\u7528 stmt.Exec \u65b9\u6cd5\u5904\u3002 \u53ef\u80fd\u7684\u9519\u8bef\u539f\u56e0\uff1a \u8c03\u7528 stmt.Close \u65b9\u6cd5\u5173\u95ed\u4e86\u51c6\u5907\u597d\u7684\u8bed\u53e5\uff0c\u5bfc\u81f4\u5176\u6307\u9488\u4e3a nil\u3002 \u51c6\u5907\u8bed\u53e5\u5728\u4f7f\u7528 stmt.Exec \u4e4b\u524d\u6ca1\u6709\u6b63\u786e\u5730\u91cd\u65b0\u51c6\u5907\u3002 Stmt.Exec \u65b9\u6cd5\u7528\u4e8e\u6267\u884c\u5df2\u51c6\u5907\u7684 SQL \u8bed\u53e5\uff0c\u800c Stmt.ExecContext \u65b9\u6cd5\u63a5\u53d7 context.Context \u53c2\u6570\uff0c\u5141\u8bb8\u5728\u6267\u884c\u65f6\u4f20\u9012\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002 \u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f MySQL \u9a71\u52a8\u4e2d\u7684 stmt.Exec\uff0c\u5e76\u4e14\u6ca1\u6709\u4f7f\u7528 stmt.ExecContext\uff0c\u5219\u9700\u8981\u786e\u4fdd\u5728\u6bcf\u6b21\u4f7f\u7528 stmt.Exec \u4e4b\u524d\u91cd\u65b0\u51c6\u5907\u8be5\u8bed\u53e5\u3002 \u56e0\u6b64\uff0c\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8bf7\u5c1d\u8bd5\u4ee5\u4e0b\u64cd\u4f5c\uff1a \u5728\u4f7f\u7528 stmt.Exec \u4e4b\u524d\u91cd\u65b0\u51c6\u5907\u8be5\u8bed\u53e5\u3002 \u786e\u4fdd\u5728\u4e0d\u518d\u9700\u8981\u51c6\u5907\u597d\u7684\u8bed\u53e5\u65f6\u5c06\u5176\u5173\u95ed\u3002 \u4ee5\u4e0a\u5c31\u662fGo + MySQL + Gin \u62a5\u9519 [&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-6862","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6862","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=6862"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6862\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}