{"id":15544,"date":"2024-11-18T20:12:44","date_gmt":"2024-11-18T12:12:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15544"},"modified":"2024-11-18T20:12:44","modified_gmt":"2024-11-18T12:12:44","slug":"go%e4%b8%adimport-%e5%af%bc%e5%85%a5%e5%8c%85%e7%9a%84%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15544\/","title":{"rendered":"Go\u4e2dimport \u5bfc\u5165\u5305\u7684\u8bed\u6cd5"},"content":{"rendered":"<h3>\u4e00\u3001\u5305\u7684\u5bfc\u5165\u8bed\u6cd5<\/h3>\n<p>\u5728\u5199Go\u4ee3\u7801\u7684\u65f6\u5019\u7ecf\u5e38\u7528\u5230import\u8fd9\u4e2a\u547d\u4ee4\u7528\u6765\u5bfc\u5165\u5305\u6587\u4ef6\uff0c\u770b\u5230\u7684\u65b9\u5f0f\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre>import(\r\n\u00a0\u00a0\u00a0 \"fmt\"\r\n)<\/pre>\n<p>\u7136\u540e\u5728\u4ee3\u7801\u91cc\u9762\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u7684\u65b9\u5f0f\u8c03\u7528<\/p>\n<pre>fmt.Println(\"hello world\")<\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2afmt\u662fGo\u8bed\u8a00\u7684\u6807\u51c6\u5e93\uff0c\u4ed6\u5176\u5b9e\u662f\u53bbGOROOT\u4e0b\u53bb\u52a0\u8f7d\u8be5\u6a21\u5757\uff0c\u5f53\u7136Go\u7684import\u8fd8\u652f\u6301\u5982\u4e0b\u4e24\u79cd\u65b9\u5f0f\u6765\u52a0\u8f7d\u81ea\u5df1\u5199\u7684\u6a21\u5757\uff1a<\/p>\n<ul>\n<li>\u76f8\u5bf9\u8def\u5f84\u00a0\u00a0\u00a0 import\u00a0\u00a0 \u201c.\/model\u201d\u00a0 \/\/\u5f53\u524d\u6587\u4ef6\u540c\u4e00\u76ee\u5f55\u7684model\u76ee\u5f55\uff0c\u4f46\u662f\u4e0d\u5efa\u8bae\u8fd9\u79cd\u65b9\u5f0fimport<\/li>\n<li>\u7edd\u5bf9\u8def\u5f84\u00a0\u00a0\u00a0 import\u00a0\u00a0 \u201cshorturl\/model\u201d\u00a0 \/\/\u52a0\u8f7dGOPATH\/src\/shorturl\/model\u6a21\u5757<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u5c55\u793a\u4e86\u4e00\u4e9bimport\u5e38\u7528\u7684\u51e0\u79cd\u65b9\u5f0f\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e9b\u7279\u6b8a\u7684import\uff0c\u8ba9\u5f88\u591a\u65b0\u624b\u5f88\u8d39\u89e3\uff0c\u4e0b\u9762\u662f\u4e09\u79cd\u5bfc\u5165\u5305\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p><strong>1\u3001<\/strong>\u70b9\u64cd\u4f5c\u00a0\u00a0 \u6709\u65f6\u5019\u4f1a\u770b\u5230\u5982\u4e0b\u7684\u65b9\u5f0f\u5bfc\u5165\u5305\u00a0\u00a0\u00a0\u00a0 import( . \u201cfmt\u201d ) ,\u8fd9\u4e2a\u70b9\u64cd\u4f5c\u7684\u542b\u4e49\u5c31\u662f\u8fd9\u4e2a\u5305\u5bfc\u5165\u4e4b\u540e\u5728\u4f60\u8c03\u7528\u8fd9\u4e2a\u5305\u7684\u51fd\u6570\u65f6\uff0c\u4f60\u53ef\u4ee5\u7701\u7565\u524d\u7f00\u7684\u5305\u540d\uff0c\u4e5f\u5c31\u662f\u524d\u9762\u4f60\u8c03\u7528\u7684fmt.Println(\u201chello world\u201d)\u00a0 \u53ef\u4ee5\u7701\u7565\u7684\u5199\u6210Println(\u201chello world\u201d)<\/p>\n<pre>import(\r\n\u00a0\u00a0\u00a0 .\"fmt\"\r\n\u00a0\u00a0\u00a0 .\"time\"\r\n\u00a0\u00a0\u00a0 .\"mysql\"\r\n)<\/pre>\n<p><strong>2\u3001<\/strong>\u522b\u540d\u64cd\u4f5c\u00a0\u00a0 \u522b\u540d\u64cd\u4f5c\u987e\u540d\u601d\u4e49\u53ef\u4ee5\u628a\u5305\u547d\u540d\u6210\u53e6\u4e00\u4e2a\u7528\u8d77\u6765\u5bb9\u6613\u8bb0\u5fc6\u7684\u540d\u5b57\uff0cimport( f \u201cfmt\u201d )\u00a0\u00a0 \u522b\u540d\u64cd\u4f5c\u8c03\u7528\u5305\u51fd\u6570\u65f6\u524d\u7f00\u53d8\u6210\u4e86\u91cd\u547d\u540d\u7684\u524d\u7f00\uff0c\u5373f.Println(\u201chello world\u201d)<\/p>\n<p><strong>3\u3001<\/strong>_\u64cd\u4f5c\u00a0\u00a0 \u8fd9\u4e2a\u64cd\u4f5c\u7ecf\u5e38\u662f\u8ba9\u5f88\u591a\u4eba\u8d39\u89e3\u7684\u4e00\u4e2a\u64cd\u4f5c\u7b26\uff0c\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2aimport<br \/>\nimport ( \u201cdatabase\/sql\u201d _ \u201cgithub.com\/ziutek\/mymysql\/godrv\u201d )<br \/>\n_\u64cd\u4f5c\u5176\u5b9e\u53ea\u662f\u5f15\u5165\u8be5\u5305\u3002\u5f53\u5bfc\u5165\u4e00\u4e2a\u5305\u65f6\uff0c\u5b83\u6240\u6709\u7684init()\u51fd\u6570\u5c31\u4f1a\u88ab\u6267\u884c\uff0c\u4f46\u6709\u4e9b\u65f6\u5019\u5e76\u975e\u771f\u7684\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u5305\uff0c\u4ec5\u4ec5\u662f\u5e0c\u671b\u5b83\u7684init()\u51fd\u6570\u88ab\u6267\u884c\u800c\u5df2\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u53ef\u4ee5\u4f7f\u7528_\u64cd\u4f5c\u5f15\u7528\u8be5\u5305\u4e86\u3002\u5373\u4f7f\u7528_\u64cd\u4f5c\u5f15\u7528\u5305\u662f\u65e0\u6cd5\u901a\u8fc7\u5305\u540d\u6765\u8c03\u7528\u5305\u4e2d\u7684\u5bfc\u51fa\u51fd\u6570\uff0c\u800c\u662f\u53ea\u662f\u4e3a\u4e86\u7b80\u5355\u7684\u8c03\u7528\u5176init\u51fd\u6570()\u3002<\/p>\n<h3><strong>\u4e8c\u3001\u5305\u7684\u5bfc\u5165\u8fc7\u7a0b\u8bf4\u660e<\/strong><\/h3>\n<p>\u7a0b\u5e8f\u7684\u521d\u59cb\u5316\u548c\u6267\u884c\u90fd\u8d77\u59cb\u4e8emain\u5305\u3002\u5982\u679cmain\u5305\u8fd8\u5bfc\u5165\u4e86\u5176\u5b83\u7684\u5305\uff0c\u90a3\u4e48\u5c31\u4f1a\u5728\u7f16\u8bd1\u65f6\u5c06\u5b83\u4eec\u4f9d\u6b21\u5bfc\u5165\u3002\u6709\u65f6\u4e00\u4e2a\u5305\u4f1a\u88ab\u591a\u4e2a\u5305\u540c\u65f6\u5bfc\u5165\uff0c\u90a3\u4e48\u5b83\u53ea\u4f1a\u88ab\u5bfc\u5165\u4e00\u6b21\uff08\u4f8b\u5982\u5f88\u591a\u5305\u53ef\u80fd\u90fd\u4f1a\u7528\u5230fmt\u5305\uff0c\u4f46\u5b83\u53ea\u4f1a\u88ab\u5bfc\u5165\u4e00\u6b21\uff0c\u56e0\u4e3a\u6ca1\u6709\u5fc5\u8981\u5bfc\u5165\u591a\u6b21\uff09\u3002\u5f53\u4e00\u4e2a\u5305\u88ab\u5bfc\u5165\u65f6\uff0c\u5982\u679c\u8be5\u5305\u8fd8\u5bfc\u5165\u4e86\u5176\u5b83\u7684\u5305\uff0c\u90a3\u4e48\u4f1a\u5148\u5c06\u5176\u5b83\u5305\u5bfc\u5165\u8fdb\u6765\uff0c\u7136\u540e\u518d\u5bf9\u8fd9\u4e9b\u5305\u4e2d\u7684\u5305\u7ea7\u5e38\u91cf\u548c\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u63a5\u7740\u6267\u884cinit\u51fd\u6570\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002\u7b49\u6240\u6709\u88ab\u5bfc\u5165\u7684\u5305\u90fd\u52a0\u8f7d\u5b8c\u6bd5\u4e86\uff0c\u5c31\u4f1a\u5f00\u59cb\u5bf9main\u5305\u4e2d\u7684\u5305\u7ea7\u5e38\u91cf\u548c\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u7136\u540e\u6267\u884cmain\u5305\u4e2d\u7684init\u51fd\u6570\uff08\u5982\u679c\u5b58\u5728\u7684\u8bdd\uff09\uff0c\u6700\u540e\u6267\u884cmain\u51fd\u6570\u3002\u4e0b\u56fe\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\uff1a<\/p>\n<p><a href=\"http:\/\/fwq.ai\/wp-content\/uploads\/2017\/04\/20140511222515968.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15545\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/20140511222515968.jpeg\" width=\"948\" height=\"419\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/20140511222515968.jpeg 948w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/20140511222515968-300x133.jpeg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/20140511222515968-768x339.jpeg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/20140511222515968-670x296.jpeg 670w\" sizes=\"auto, (max-width: 948px) 100vw, 948px\" title=\"Go\u4e2dimport \u5bfc\u5165\u5305\u7684\u8bed\u6cd5\u63d2\u56fe\" alt=\"Go\u4e2dimport \u5bfc\u5165\u5305\u7684\u8bed\u6cd5\u63d2\u56fe\" \/><\/a><\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ecb\u7ecd\u6211\u4eec\u4e86\u89e3\u4e86import\u7684\u65f6\u5019\u5176\u5b9e\u662f\u6267\u884c\u4e86\u8be5\u5305\u91cc\u9762\u7684init\u51fd\u6570\uff0c\u521d\u59cb\u5316\u4e86\u91cc\u9762\u7684\u53d8\u91cf\uff0c_\u64cd\u4f5c\u53ea\u662f\u8bf4\u8be5\u5305\u5f15\u5165\u4e86\uff0c\u53ea\u521d\u59cb\u5316\u91cc\u9762\u7684init\u51fd\u6570\u548c\u4e00\u4e9b\u53d8\u91cf\uff0c\u4e0d\u80fd\u901a\u8fc7\u5305\u540d\u6765\u8c03\u7528\u5176\u5b83\u7684\u51fd\u6570\uff0c\u8fd9\u6709\u4ec0\u4e48\u7528\u5462\uff1f\u5f80\u5f80\u8fd9\u4e9binit\u51fd\u6570\u91cc\u9762\u662f\u6ce8\u518c\u81ea\u5df1\u5305\u91cc\u9762\u7684\u5f15\u64ce\uff0c\u8ba9\u5916\u90e8\u53ef\u4ee5\u65b9\u4fbf\u7684\u4f7f\u7528\uff0c\u5c31\u5f88\u591a\u5b9e\u73b0database\/sql\u7684\u5f15\u8d77\uff0c\u5728init\u51fd\u6570\u91cc\u9762\u90fd\u662f\u8c03\u7528\u4e86sql.Register(name string, driver driver.Driver)\u6ce8\u518c\u81ea\u5df1\uff0c\u7136\u540e\u5916\u90e8\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5305\u7684\u5bfc\u5165\u8bed\u6cd5 \u5728\u5199Go\u4ee3\u7801\u7684\u65f6\u5019\u7ecf\u5e38\u7528\u5230import\u8fd9\u4e2a\u547d\u4ee4\u7528\u6765\u5bfc\u5165\u5305\u6587\u4ef6\uff0c\u770b\u5230\u7684\u65b9\u5f0f\u53c2\u8003\u5982\u4e0b\uff1a &nbsp; import( \u00a0\u00a0\u00a0 &#8220;fmt&#8221; ) \u7136\u540e\u5728\u4ee3\u7801\u91cc\u9762\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u7684\u65b9\u5f0f\u8c03\u7528 fmt.Println(&#8220;hello world&#8221;) \u4e0a\u9762\u8fd9\u4e2afmt\u662fGo\u8bed\u8a00\u7684\u6807\u51c6\u5e93\uff0c\u4ed6\u5176\u5b9e\u662f\u53bbGOROOT\u4e0b\u53bb\u52a0\u8f7d\u8be5\u6a21\u5757\uff0c\u5f53\u7136Go\u7684import\u8fd8\u652f\u6301\u5982\u4e0b\u4e24\u79cd\u65b9\u5f0f\u6765\u52a0\u8f7d\u81ea\u5df1\u5199\u7684\u6a21\u5757\uff1a \u76f8\u5bf9\u8def\u5f84\u00a0\u00a0\u00a0 import\u00a0\u00a0 \u201c.\/model\u201d\u00a0 \/\/\u5f53\u524d\u6587\u4ef6\u540c\u4e00\u76ee\u5f55\u7684model\u76ee\u5f55\uff0c\u4f46\u662f\u4e0d\u5efa\u8bae\u8fd9\u79cd\u65b9\u5f0fimport \u7edd\u5bf9\u8def\u5f84\u00a0\u00a0\u00a0 import\u00a0\u00a0 \u201cshorturl\/model\u201d\u00a0 \/\/\u52a0\u8f7dGOPATH\/src\/shorturl\/model\u6a21\u5757 \u4e0a\u9762\u5c55\u793a\u4e86\u4e00\u4e9bimport\u5e38\u7528\u7684\u51e0\u79cd\u65b9\u5f0f\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e9b\u7279\u6b8a\u7684import\uff0c\u8ba9\u5f88\u591a\u65b0\u624b\u5f88\u8d39\u89e3\uff0c\u4e0b\u9762\u662f\u4e09\u79cd\u5bfc\u5165\u5305\u7684\u4f7f\u7528\u65b9\u6cd5\u3002 1\u3001\u70b9\u64cd\u4f5c\u00a0\u00a0 \u6709\u65f6\u5019\u4f1a\u770b\u5230\u5982\u4e0b\u7684\u65b9\u5f0f\u5bfc\u5165\u5305\u00a0\u00a0\u00a0\u00a0 import( . \u201cfmt\u201d ) ,\u8fd9\u4e2a\u70b9\u64cd\u4f5c\u7684\u542b\u4e49\u5c31\u662f\u8fd9\u4e2a\u5305\u5bfc\u5165\u4e4b\u540e\u5728\u4f60\u8c03\u7528\u8fd9\u4e2a\u5305\u7684\u51fd\u6570\u65f6\uff0c\u4f60\u53ef\u4ee5\u7701\u7565\u524d\u7f00\u7684\u5305\u540d\uff0c\u4e5f\u5c31\u662f\u524d\u9762\u4f60\u8c03\u7528\u7684fmt.Println(\u201chello world\u201d)\u00a0 \u53ef\u4ee5\u7701\u7565\u7684\u5199\u6210Println(\u201chello world\u201d) import( \u00a0\u00a0\u00a0 .&#8221;fmt&#8221; \u00a0\u00a0\u00a0 .&#8221;time&#8221; \u00a0\u00a0\u00a0 .&#8221;mysql&#8221; ) 2\u3001\u522b\u540d\u64cd\u4f5c\u00a0\u00a0 \u522b\u540d\u64cd\u4f5c\u987e\u540d\u601d\u4e49\u53ef\u4ee5\u628a\u5305\u547d\u540d\u6210\u53e6\u4e00\u4e2a\u7528\u8d77\u6765\u5bb9\u6613\u8bb0\u5fc6\u7684\u540d\u5b57\uff0cimport( f \u201cfmt\u201d )\u00a0\u00a0 \u522b\u540d\u64cd\u4f5c\u8c03\u7528\u5305\u51fd\u6570\u65f6\u524d\u7f00\u53d8\u6210\u4e86\u91cd\u547d\u540d\u7684\u524d\u7f00\uff0c\u5373f.Println(\u201chello world\u201d) 3\u3001_\u64cd\u4f5c\u00a0\u00a0 \u8fd9\u4e2a\u64cd\u4f5c\u7ecf\u5e38\u662f\u8ba9\u5f88\u591a\u4eba\u8d39\u89e3\u7684\u4e00\u4e2a\u64cd\u4f5c\u7b26\uff0c\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2aimport import ( \u201cdatabase\/sql\u201d _ \u201cgithub.com\/ziutek\/mymysql\/godrv\u201d ) _\u64cd\u4f5c\u5176\u5b9e\u53ea\u662f\u5f15\u5165\u8be5\u5305\u3002\u5f53\u5bfc\u5165\u4e00\u4e2a\u5305\u65f6\uff0c\u5b83\u6240\u6709\u7684init()\u51fd\u6570\u5c31\u4f1a\u88ab\u6267\u884c\uff0c\u4f46\u6709\u4e9b\u65f6\u5019\u5e76\u975e\u771f\u7684\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u5305\uff0c\u4ec5\u4ec5\u662f\u5e0c\u671b\u5b83\u7684init()\u51fd\u6570\u88ab\u6267\u884c\u800c\u5df2\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u53ef\u4ee5\u4f7f\u7528_\u64cd\u4f5c\u5f15\u7528\u8be5\u5305\u4e86\u3002\u5373\u4f7f\u7528_\u64cd\u4f5c\u5f15\u7528\u5305\u662f\u65e0\u6cd5\u901a\u8fc7\u5305\u540d\u6765\u8c03\u7528\u5305\u4e2d\u7684\u5bfc\u51fa\u51fd\u6570\uff0c\u800c\u662f\u53ea\u662f\u4e3a\u4e86\u7b80\u5355\u7684\u8c03\u7528\u5176init\u51fd\u6570()\u3002 \u4e8c\u3001\u5305\u7684\u5bfc\u5165\u8fc7\u7a0b\u8bf4\u660e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-15544","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15544","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=15544"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15544\/revisions"}],"predecessor-version":[{"id":15546,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15544\/revisions\/15546"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}