{"id":6854,"date":"2024-11-14T12:42:27","date_gmt":"2024-11-14T04:42:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6854\/"},"modified":"2024-11-14T12:42:27","modified_gmt":"2024-11-14T04:42:27","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9c%a8%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bf%ae%e6%94%b9%e6%8c%87%e9%92%88%e5%8f%98%e9%87%8f%e7%9a%84%e5%80%bc%ef%bc%8c%e5%87%bd%e6%95%b0%e5%a4%96%e9%83%a8%e5%8d%b4%e6%97%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6854\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u51fd\u6570\u5916\u90e8\u5374\u65e0\u6cd5\u83b7\u53d6\u5230\u4fee\u6539\u540e\u7684\u503c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172999586519779.jpg\" class=\"aligncenter\" title=\"\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u51fd\u6570\u5916\u90e8\u5374\u65e0\u6cd5\u83b7\u53d6\u5230\u4fee\u6539\u540e\u7684\u503c\uff1f\u63d2\u56fe\" alt=\"\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u51fd\u6570\u5916\u90e8\u5374\u65e0\u6cd5\u83b7\u53d6\u5230\u4fee\u6539\u540e\u7684\u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u6307\u9488\u503c\u65e0\u6cd5\u5728\u51fd\u6570\u5916\u66f4\u6539\uff1f<\/strong><\/p>\n<p>\u5982\u7ed9\u5b9a\u4ee3\u7801\u6240\u793a\uff0c\u6211\u4eec\u5728 initdb \u51fd\u6570\u4e2d\u5bf9\u6307\u9488\u53d8\u91cf\u8d4b\u503c\uff0c\u4f46 mn \u51fd\u6570\u4e2d\u6253\u5370\u65f6\u4ecd\u7136\u663e\u793a\u4e3a nil\u3002<\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u8986\u76d6\u53c2\u6570\u53d8\u91cf\uff0c\u5373\u4f7f\u8fd9\u4e24\u4e2a\u53d8\u91cf\u540c\u540d\u3002initdb \u51fd\u6570\u4e2d\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf db \u662f\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u5b83\u8986\u76d6\u4e86\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u7684 db\u3002<\/p>\n<p>\u5f53\u5bf9\u5c40\u90e8\u53d8\u91cf db \u8d4b\u503c\u65f6\uff0c\u5b83\u5e76\u4e0d\u5f71\u54cd\u51fd\u6570\u53c2\u6570 db \u7684\u503c\u3002\u56e0\u6b64\uff0cmain \u51fd\u6570\u6253\u5370\u65f6\u83b7\u53d6\u7684\u662f\u53c2\u6570 db \u7684\u503c\uff0c\u4e5f\u5c31\u662f nil\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u8981\u66f4\u6539\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u7684\u89e3\u5f15\u7528\u7b26\u53f7 *\u3002\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre>func initDB(db **sql.DB) {\n    \/\/ \u4f7f\u7528 *db \u6765\u89e3\u5f15\u7528\u6307\u9488\u5e76\u5bf9\u5176\u503c\u8fdb\u884c\u64cd\u4f5c\n    *db, err = sql.Open(\"mysql\", \"root:123456@tcp(127.0.0.1:3306)\/data\")\n    checkErr(err)\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u51fd\u6570\u5916\u90e8\u5374\u65e0\u6cd5\u83b7\u53d6\u5230\u4fee\u6539\u540e\u7684\u503c\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>\u4e3a\u4ec0\u4e48\u6307\u9488\u503c\u65e0\u6cd5\u5728\u51fd\u6570\u5916\u66f4\u6539\uff1f \u5982\u7ed9\u5b9a\u4ee3\u7801\u6240\u793a\uff0c\u6211\u4eec\u5728 initdb \u51fd\u6570\u4e2d\u5bf9\u6307\u9488\u53d8\u91cf\u8d4b\u503c\uff0c\u4f46 mn \u51fd\u6570\u4e2d\u6253\u5370\u65f6\u4ecd\u7136\u663e\u793a\u4e3a nil\u3002 \u539f\u56e0\uff1a \u5728 go \u8bed\u8a00\u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u8986\u76d6\u53c2\u6570\u53d8\u91cf\uff0c\u5373\u4f7f\u8fd9\u4e24\u4e2a\u53d8\u91cf\u540c\u540d\u3002initdb \u51fd\u6570\u4e2d\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf db \u662f\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u5b83\u8986\u76d6\u4e86\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u7684 db\u3002 \u5f53\u5bf9\u5c40\u90e8\u53d8\u91cf db \u8d4b\u503c\u65f6\uff0c\u5b83\u5e76\u4e0d\u5f71\u54cd\u51fd\u6570\u53c2\u6570 db \u7684\u503c\u3002\u56e0\u6b64\uff0cmain \u51fd\u6570\u6253\u5370\u65f6\u83b7\u53d6\u7684\u662f\u53c2\u6570 db \u7684\u503c\uff0c\u4e5f\u5c31\u662f nil\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u8981\u66f4\u6539\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6307\u9488\u7684\u89e3\u5f15\u7528\u7b26\u53f7 *\u3002\u5177\u4f53\u5982\u4e0b\uff1a func initDB(db **sql.DB) { \/\/ \u4f7f\u7528 *db \u6765\u89e3\u5f15\u7528\u6307\u9488\u5e76\u5bf9\u5176\u503c\u8fdb\u884c\u64cd\u4f5c *db, err = sql.Open(&#8220;mysql&#8221;, &#8220;root:123456@tcp(127.0.0.1:3306)\/data&#8221;) checkErr(err) } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u6307\u9488\u53d8\u91cf\u7684\u503c\uff0c\u51fd\u6570\u5916\u90e8\u5374\u65e0\u6cd5\u83b7\u53d6\u5230\u4fee\u6539\u540e\u7684\u503c\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-6854","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6854","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=6854"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6854\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}