{"id":6817,"date":"2024-11-14T08:27:49","date_gmt":"2024-11-14T00:27:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6817\/"},"modified":"2024-11-14T08:27:49","modified_gmt":"2024-11-14T00:27:49","slug":"go%e8%af%ad%e8%a8%80%e5%87%bd%e6%95%b0%e4%b8%ad%e6%8c%87%e9%92%88%e8%b5%8b%e5%80%bc%e4%b8%ba%e4%bd%95%e5%a4%b1%e6%95%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6817\/","title":{"rendered":"Go\u8bed\u8a00\u51fd\u6570\u4e2d\u6307\u9488\u8d4b\u503c\u4e3a\u4f55\u5931\u6548\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172994725735163.jpg\" class=\"aligncenter\" title=\"Go\u8bed\u8a00\u51fd\u6570\u4e2d\u6307\u9488\u8d4b\u503c\u4e3a\u4f55\u5931\u6548\uff1f\u63d2\u56fe\" alt=\"Go\u8bed\u8a00\u51fd\u6570\u4e2d\u6307\u9488\u8d4b\u503c\u4e3a\u4f55\u5931\u6548\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6307\u9488\u8d4b\u503c\u4e3a\u4f55\u5931\u6548<\/strong><\/p>\n<p>\u4e3a\u4ec0\u4e48\u5728go\u4e2d\uff0c\u5c06\u6307\u9488\u53d8\u91cf\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u51fd\u6570\u5185\u5bf9\u6307\u9488\u503c\u8fdb\u884c\u4fee\u6539\u540e\uff0c\u5916\u90e8\u65e0\u6cd5\u83b7\u53d6\u4fee\u6539\u540e\u7684\u503c\uff1f<\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>var db *sql.DB\n\nfunc main() {\n    initDB(db)\n\n    fmt.Println(db) \/\/ \u8f93\u51fa &lt;nil&gt;\n}\n\nfunc initDB(db *sql.DB) {\n    db, err := sql.Open(\"mysql\", \"root:123456@tcp(127.0.0.1:3306)\/data\")\n    checkErr(err)\n\n    db.SetMaxOpenConns(100)\n    db.SetMaxIdleConns(100)\n    db.SetConnMaxLifetime(time.Minute * 3)\n\n    if err := db.Ping(); err != nil {\n        checkErr(err)\n    }\n\n    fmt.Println(db) \/\/ \u8f93\u51fa\u6b63\u786e\u7684\u503c\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cmn() \u4e2d\u8f93\u51fa db \u4e3a \uff0c\u5373\u4f7f initdb() \u4e2d\u5df2\u6210\u529f\u4e3a db \u8d4b\u503c\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a <\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>go \u4e2d\u5c40\u90e8\u53d8\u91cf\u7684\u8986\u76d6<\/strong><\/p>\n<p>go \u8bed\u8a00\u4e2d\uff0c\u51fd\u6570\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u540d\u4f1a\u8986\u76d6\u51fd\u6570\u53c2\u6570\u7684\u540d\u79f0\u3002\u8fd9\u610f\u5473\u7740 initdb() \u4e2d\u58f0\u660e\u7684\u53d8\u91cf db \u4e0e main() \u4e2d\u7684\u53d8\u91cf db \u662f\u4e0d\u540c\u7684\u53d8\u91cf\u3002<\/p>\n<p>initdb() \u4e2d\uff0c\u51fd\u6570\u5c06\u4e00\u4e2a\u65b0\u7684\u503c\u5206\u914d\u7ed9\u4e86\u5c40\u90e8\u53d8\u91cf db\uff0c\u4f46\u5e76\u672a\u4fee\u6539\u51fd\u6570\u53c2\u6570 db \u6307\u5411\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0cmain() \u4e2d\u7684\u53d8\u91cf db \u4ecd\u7136\u4fdd\u7559\u7740\u521d\u59cb\u5316\u65f6\u7684 \u503c\u3002 <\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u6307\u9488\u63a5\u6536\u5668\uff1a\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u51fd\u6570\u53c2\u6570\u6307\u5411\u7684\u5bf9\u8c61\u3002<\/li>\n<li>\u4fee\u6539\u51fd\u6570\u7684\u53c2\u6570\uff1a\u5c06\u53c2\u6570\u7c7b\u578b\u4ece *t \u4fee\u6539\u4e3a t\uff0c\u76f4\u63a5\u8fd4\u56de\u65b0\u7684\u503c\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u91c7\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u548c\u51fd\u6570\u5916\u4fee\u6539\u6307\u9488\u6307\u5411\u7684\u5bf9\u8c61\u7684\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGo\u8bed\u8a00\u51fd\u6570\u4e2d\u6307\u9488\u8d4b\u503c\u4e3a\u4f55\u5931\u6548\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>\u6307\u9488\u8d4b\u503c\u4e3a\u4f55\u5931\u6548 \u4e3a\u4ec0\u4e48\u5728go\u4e2d\uff0c\u5c06\u6307\u9488\u53d8\u91cf\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u51fd\u6570\u5185\u5bf9\u6307\u9488\u503c\u8fdb\u884c\u4fee\u6539\u540e\uff0c\u5916\u90e8\u65e0\u6cd5\u83b7\u53d6\u4fee\u6539\u540e\u7684\u503c\uff1f \u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a var db *sql.DB func main() { initDB(db) fmt.Println(db) \/\/ \u8f93\u51fa &lt;nil&gt; } func initDB(db *sql.DB) { db, err := sql.Open(&#8220;mysql&#8221;, &#8220;root:123456@tcp(127.0.0.1:3306)\/data&#8221;) checkErr(err) db.SetMaxOpenConns(100) db.SetMaxIdleConns(100) db.SetConnMaxLifetime(time.Minute * 3) if err := db.Ping(); err != nil { checkErr(err) } fmt.Println(db) \/\/ \u8f93\u51fa\u6b63\u786e\u7684\u503c } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cmn() \u4e2d\u8f93\u51fa db \u4e3a \uff0c\u5373\u4f7f initdb() \u4e2d\u5df2\u6210\u529f\u4e3a db \u8d4b\u503c\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b [&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-6817","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6817","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=6817"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6817\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}