{"id":6834,"date":"2024-11-14T12:21:53","date_gmt":"2024-11-14T04:21:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6834\/"},"modified":"2024-11-14T12:21:53","modified_gmt":"2024-11-14T04:21:53","slug":"golang-%e4%bd%bf%e7%94%a8-mysql-%e5%92%8c-gin-%e6%97%b6%e5%87%ba%e7%8e%b0%e5%86%85%e5%ad%98%e9%94%99%e8%af%af%ef%bc%9a%e5%85%b3%e9%97%ad%e8%af%ad%e5%8f%a5%e5%90%8e%e4%bb%8d%e4%bd%bf%e7%94%a8%e5%ae%83","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6834\/","title":{"rendered":"Golang \u4f7f\u7528 MySQL \u548c Gin \u65f6\u51fa\u73b0\u5185\u5b58\u9519\u8bef\uff1a\u5173\u95ed\u8bed\u53e5\u540e\u4ecd\u4f7f\u7528\u5b83\u4f1a\u5bfc\u81f4\u4ec0\u4e48\u95ee\u9898\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172998774423503.jpg\" class=\"aligncenter\" title=\"Golang \u4f7f\u7528 MySQL \u548c Gin \u65f6\u51fa\u73b0\u5185\u5b58\u9519\u8bef\uff1a\u5173\u95ed\u8bed\u53e5\u540e\u4ecd\u4f7f\u7528\u5b83\u4f1a\u5bfc\u81f4\u4ec0\u4e48\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"Golang \u4f7f\u7528 MySQL \u548c Gin \u65f6\u51fa\u73b0\u5185\u5b58\u9519\u8bef\uff1a\u5173\u95ed\u8bed\u53e5\u540e\u4ecd\u4f7f\u7528\u5b83\u4f1a\u5bfc\u81f4\u4ec0\u4e48\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u4e2d\u4f7f\u7528  \u548c gin \u65f6\u9047\u5230\u5185\u5b58\u9519\u8bef<\/strong><\/p>\n<p>\u5728 golang \u9879\u76ee\u4e2d\u9047\u5230\u4e86\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>panic recovered: runtime error: invalid memory address or nil pointer dereference\n\/snap\/go\/8489\/src\/runtime\/panic.go:221 (0x44a446)\n        panicmem: panic(memoryError)\n\/snap\/go\/8489\/src\/runtime\/signal_unix.go:735 (0x44a416)\n        sigpanic: panicmem()\n\/snap\/go\/8489\/src\/database\/sql\/sql.go:2578 (0x4f30c2)\n        (*Stmt).ExecContext: s.closemu.RLock()\n\/snap\/go\/8489\/src\/database\/sql\/sql.go:2610 (0x840fd4)\n        (*Stmt).Exec: return s.ExecContext(context.Background(), args...)\n\/root\/onlineUsers\/hello.go:63 (0x840f36)\n        main.func1: _, err = stmt.Exec(req.IdCode, c.Request.RemoteAddr)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6000\u7591\u662f\u56e0\u4e3a\u4f7f\u7528\u4e86 mysql \u9a71\u52a8\u4e2d\u7684 stmt.exec \u800c\u6ca1\u6709\u4f7f\u7528 stmt.execcontext\uff0c\u4f46\u7b54\u6848\u8868\u660e\u95ee\u9898\u51fa\u5728\u522b\u5904\u3002<\/p>\n<p><strong>\u6b63\u786e\u7b54\u6848\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u7b54\u6848\u6307\u51fa\uff0c\u5728\u5173\u95ed\u8bed\u53e5\uff08stmt\uff09\u540e\u4ecd\u5728\u4f7f\u7528\u5b83\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u9519\u8bef\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGolang \u4f7f\u7528 MySQL \u548c Gin \u65f6\u51fa\u73b0\u5185\u5b58\u9519\u8bef\uff1a\u5173\u95ed\u8bed\u53e5\u540e\u4ecd\u4f7f\u7528\u5b83\u4f1a\u5bfc\u81f4\u4ec0\u4e48\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>\u4e2d\u4f7f\u7528 \u548c gin \u65f6\u9047\u5230\u5185\u5b58\u9519\u8bef \u5728 golang \u9879\u76ee\u4e2d\u9047\u5230\u4e86\u4ee5\u4e0b\u9519\u8bef\uff1a panic recovered: runtime error: invalid memory address or nil pointer dereference \/snap\/go\/8489\/src\/runtime\/panic.go:221 (0x44a446) panicmem: panic(memoryError) \/snap\/go\/8489\/src\/runtime\/signal_unix.go:735 (0x44a416) sigpanic: panicmem() \/snap\/go\/8489\/src\/database\/sql\/sql.go:2578 (0x4f30c2) (*Stmt).ExecContext: s.closemu.RLock() \/snap\/go\/8489\/src\/database\/sql\/sql.go:2610 (0x840fd4) (*Stmt).Exec: return s.ExecContext(context.Background(), args&#8230;) \/root\/onlineUsers\/hello.go:63 (0x840f36) main.func1: _, err = stmt.Exec(req.IdCode, c.Request.RemoteAddr) \u767b\u5f55\u540e\u590d\u5236 \u6000\u7591\u662f\u56e0\u4e3a\u4f7f\u7528\u4e86 mysql \u9a71\u52a8\u4e2d\u7684 stmt.exec \u800c\u6ca1\u6709\u4f7f\u7528 stmt.execcontext\uff0c\u4f46\u7b54\u6848\u8868\u660e\u95ee\u9898\u51fa\u5728\u522b\u5904\u3002 \u6b63\u786e\u7b54\u6848\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u7b54\u6848\u6307\u51fa\uff0c\u5728\u5173\u95ed\u8bed\u53e5\uff08stmt\uff09\u540e\u4ecd\u5728\u4f7f\u7528\u5b83\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u9519\u8bef\u3002 \u4ee5\u4e0a\u5c31\u662fGolang [&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-6834","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6834","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=6834"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6834\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}