{"id":2895,"date":"2021-02-23T22:36:05","date_gmt":"2021-02-23T14:36:05","guid":{"rendered":"http:\/\/blog.ywdevops.cn\/?p=2895"},"modified":"2025-08-27T16:13:30","modified_gmt":"2025-08-27T08:13:30","slug":"go-6","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2021\/02\/23\/go-6\/","title":{"rendered":"Go\u8bed\u8a00\u4e4b\u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u662f\u7ec4\u7ec7\u597d\u7684\u3001\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u3001\u7528\u6765\u5b9e\u73b0\u5355\u4e00\u6216\u76f8\u5173\u8054\u529f\u80fd\u7684\u4ee3\u7801\u6bb5\uff0c\u5176\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7684\u6a21\u5757\u6027\u548c\u4ee3\u7801\u7684\u91cd\u590d\u5229\u7528\u7387<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u652f\u6301\u666e\u901a\u51fd\u6570\u3001\u533f\u540d\u51fd\u6570\u548c\u95ed\u5305\uff0c\u4ece\u8bbe\u8ba1\u4e0a\u5bf9\u51fd\u6570\u8fdb\u884c\u4e86\u4f18\u5316\u548c\u6539\u8fdb\uff0c\u8ba9\u51fd\u6570\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u65b9\u4fbf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7684\u51fd\u6570\u672c\u8eab\u53ef\u4ee5\u4f5c\u4e3a\u503c\u8fdb\u884c\u4f20\u9012,\u652f\u6301\u533f\u540d\u51fd\u6570\u548c\u95ed\u5305\uff08 closure \uff09,\u51fd\u6570\u53ef\u4ee5\u6ee1\u8db3\u63a5\u53e3<\/p>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\" style=\"font-size:clamp(16.293px, 1.018rem + ((1vw - 3.2px) * 0.68), 25px);px\"><strong>\u58f0\u660e\u51fd\u6570<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u666e\u901a\u51fd\u6570\u9700\u8981\u5148\u58f0\u660e\u624d\u80fd\u8c03\u7528\u3002\u4e00\u4e2a\u51fd\u6570\u7684\u58f0\u660e\u5305\u62ec\u53c2\u6570\u548c\u51fd\u6570\u540d\u7b49\uff0c\u7f16\u8bd1\u5668\u901a\u8fc7\u58f0\u660e\u624d\u80fd\u4e86\u89e3\u51fd\u6570\u5e94\u8be5\u600e\u6837\u5728\u8c03\u7528\u4ee3\u7801\u548c\u51fd\u6570\u4f53\u4e4b\u95f4\u4f20\u9012\u53c2\u6570\u548c\u8fd4\u56de\u53c2\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1\u3001\u666e\u901a\u51fd\u6570\u7684\u58f0\u660e\u5f62\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7684\u51fd\u6570\u58f0\u660e\u4ee5func \u6807\u8bc6\uff0c\u540e\u9762\u7d27\u63a5\u7740\u51fd\u6570\u540d\u3001\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u53c2\u6570\u5217\u8868\u53ca\u51fd\u6570\u4f53\uff0c\u5177\u4f53\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func \u51fd\u6570\u540d\uff08\u53c2\u6570\u5217\u8868\uff09\uff08\u8fd4\u56de\u53c2\u6570\u5217\u8868\uff09\uff5b\n\u51fd\u6570\u4f53\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u51fd\u6570\u540d\uff1a\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u753b\u7ebf\u7ec4\u6210\u3002\u5176\u4e2d\uff0c\u51fd\u6570\u540d\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u4e0d\u80fd\u4e3a\u6570\u5b57\u3002\u5728\u540c\u4e00\u4e2a\u5305\u5185\uff0c\u51fd\u6570\u540d\u79f0\u4e0d\u80fd\u91cd\u540d\uff0c\u8c03\u7528\u53ef\u80fd\u4f1a\u8c03\u7528\u9519<\/li>\n\n\n\n<li>\u53c2\u6570\u5217\u8868\uff1a \u4e00\u4e2a\u53c2\u6570\u7531\u53c2\u6570\u53d8\u91cf\u548c\u53c2\u6570\u7c7b\u578b\u7ec4\u6210\uff0c\u4f8b\u5982\uff1afunc foo ( a int , b string )\uff0c\u5176\u4e2d\uff0c\u53c2\u6570\u5217\u8868\u4e2d\u7684\u53d8\u91cf\u4f5c\u4e3a\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u800c\u5b58\u5728\u3002<\/li>\n\n\n\n<li>\u8fd4\u56de\u53c2\u6570\u5217\u8868\uff1a\u53ef\u4ee5\u662f\u8fd4\u56de\u503c\u7c7b\u578b\u5217\u8868\uff0c\u4e5f\u53ef\u4ee5\u662f\u7c7b\u4f3c\u53c2\u6570\u5217\u8868\u4e2d\u53d8\u91cf\u540d\u548c\u7c7b\u578b\u540d\u7684\u7ec4\u5408\u3002<strong>\u51fd\u6570\u5728\u58f0\u660e\u6709\u8fd4\u56de\u503c\u65f6\uff0c\u5fc5\u987b\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528return\u8bed\u53e5\u63d0\u4f9b\u8fd4\u56de\u503c\u5217\u8868<\/strong><\/li>\n\n\n\n<li>\u51fd\u6570\u4f53\uff1a\u80fd\u591f\u88ab\u91cd\u590d\u8c03\u7528\u7684\u4ee3\u7801\u7247\u6bb5<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2\u3001\u53c2\u6570\u7c7b\u578b\u7684\u7b80\u5199\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u53c2\u6570\u5217\u8868\u4e2d\uff0c\u5982\u6709\u591a\u4e2a\u53c2\u6570\u53d8\u91cf\uff0c\u5219\u4ee5\u9017\u53f7\u5206\u9694\uff1b\u5982\u679c\u76f8\u90bb\u53d8\u91cf\u662f\u540c\u7c7b\u578b\uff0c\u5219\u53ef\u4ee5\u5c06\u7c7b\u578b\u7701\u7565\u3002\u4f8b\u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func add (a, b int \uff09int {\nreturn a + b\n}\n\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c a \u548cb \u7684\u53c2\u6570\u5747\u4e3aint \u7c7b\u578b\uff0c\u56e0\u6b64\u53ef\u4ee5\u7701\u7565a \u7684\u7c7b\u578b\uff0c\u5728b \u540e\u9762\u6709\u7c7b\u578b\u8bf4\u660e\uff0c\u8fd9\u4e2a\u7c7b\u578b\u4e5f\u662fa \u7684\u7c7b\u578b\u3002<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u652f\u6301\u591a\u8fd4\u56de\u503c\uff0c\u591a\u8fd4\u56de\u503c\u80fd\u65b9\u4fbf\u5730\u83b7\u5f97\u51fd\u6570\u6267\u884c\u540e\u7684\u591a\u4e2a\u8fd4\u56de\u53c2\u6570\uff0c Go \u8bed\u8a00\u7ecf\u5e38\u4f7f\u7528\u591a\u8fd4\u56de\u503c\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u8fd4\u56de\u53c2\u6570\u8fd4\u56de\u51fd\u6570\u6267\u884c\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>conn , err := connectToNetwork()\n\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c connectToNetwork \u8fd4\u56de\u4e24\u4e2a\u53c2\u6570\uff0c conn \u8868\u793a\u8fde\u63a5\u5bf9\u8c61\uff0c err\u8fd4\u56de\u9519\u8bef<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u65e2\u652f\u6301\u5b89\u5168\u6307\u9488\uff0c\u4e5f\u652f\u6301\u591a\u8fd4\u56de\u503c\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u51fd\u6570\u8fdb\u884c\u903b\u8f91\u7f16\u5199\u65f6\u66f4\u4e3a\u65b9\u4fbf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.1 \u540c\u4e00\u79cd\u7c7b\u578b\u8fd4\u56de\u503c\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8fd4\u56de\u503c\u662f\u540c\u4e00\u79cd\u7c7b\u578b\uff0c\u5219\u7528\u62ec\u53f7\u5c06\u591a\u4e2a\u8fd4\u56de\u503c\u7c7b\u578b\u62ec\u8d77\u6765\uff0c\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u4f7f\u7528return\u8bed\u53e5\u8fd4\u56de\u65f6\uff0c\u503c\u5217\u8868\u7684\u987a\u5e8f\u9700\u8981\u4e0e\u51fd\u6570\u58f0\u660e\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e00\u81f4\uff0c\u4ee3\u7801\u5982\u4e0b:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"516\" height=\"245\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/33-2.png\" alt=\"\" class=\"wp-image-3219\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/33-2.png 516w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/33-2-300x142.png 300w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u4e0a\u9762test()\u51fd\u6570\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a\u90fd\u662fint\u7c7b\u578b\u8fd4\u56de\u503c\uff0c\u90a3\u4e48return\u4e5f\u8981\u76f8\u5e94\u7684\u6709\u5bf9\u5e94\u7684\u8fd4\u56de\u503c\uff0c\u5e76\u548c\u5b9a\u4e49\u987a\u5e8f\u4e00\u4e00\u5bf9\u5e94<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.2\uff0c\u4e0d\u540c\u7c7b\u578b\u8fd4\u56de\u503c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"225\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/4.png\" alt=\"\" class=\"wp-image-3590\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/4.png 574w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/4-300x118.png 300w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"535\" height=\"118\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/5.png\" alt=\"\" class=\"wp-image-3591\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/5.png 535w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/5-300x66.png 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\uff1a\u51fd\u6570\u867d\u7136\u6709\u8fd4\u56de\u503c\uff0c\u4f46\u662f\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4e0d\u5b9a\u4e49\u63a5\u6536\u503c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"388\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/6-3.jpg\" alt=\"\" class=\"wp-image-16730\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/6-3.jpg 690w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/6-3-300x169.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/6-3-600x337.jpg 600w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u53ef\u4ee5\u6b63\u5e38\u6253\u5370\u800c\u4e0d\u62a5\u9519\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"61\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/7-3.jpg\" alt=\"\" class=\"wp-image-16731\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/7-3.jpg 688w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/7-3-300x27.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/04\/7-3-600x53.jpg 600w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4\u3001\u5b9a\u4e49\u4e0d\u5b9a\u53c2\u6570\u51fd\u6570<\/strong>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u5b9a\u53c2\u6570\u51fd\u6570\u5c31\u662f\u53c2\u6570\u4e0d\u786e\u5b9a\u7684\u51fd\u6570\uff0c\u8868\u793a\u53ef\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684int\u53c2\u6570\uff0c\u5728Go\u8bed\u8a00\u4e2d\uff0c\u80fd\u591f\u63a5\u53d7\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\uff0c\u4f46\u4ed6\u4eec\u7684\u7c7b\u578b\u5fc5\u987b\u548c\u51fd\u6570\u7b7e\u540d\u6307\u5b9a\u7684\u7c7b\u578b\u76f8\u540c\uff0c\u8981\u6307\u5b9a\u4e0d\u5b9a\u53c2\u6570\uff0c\u53ef\u4f7f\u7528\u4e09\u4e2a\u70b9(&#8230;)\uff0c\u4e0d\u5b9a\u53c2\u6570\u53ea\u80fd\u51fa\u73b0\u5728\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\uff0c\u56e0\u6b64\u4e00\u4e2a\u51fd\u6570\u4e2d\u53ea\u80fd\u6709\u4e00\u4e2a\u4e0d\u5b9a\u53c2\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u4e0b\u9762\u4f8b\u5b50\u4e2d\u8868\u793a\u5b9a\u4e49\u51fd\u6570\uff0c\u53c2\u6570\u4e3a\u4e0d\u5b9a\u53c2\u6570\u7684int\u7c7b\u578b<\/mark>\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"316\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/10-4.png\" alt=\"\" class=\"wp-image-11045\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/10-4.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/10-4-300x134.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/10-4-600x268.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"84\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/11-3.png\" alt=\"\" class=\"wp-image-11047\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/11-3.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/11-3-300x35.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/12\/11-3-600x71.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0c\u4e0d\u5b9a\u53c2\u6570\u7684\u7c7b\u578b\u5b9e\u9645\u662f\u4e00\u4e2aint\u7c7b\u578b\u7684\u5207\u7247<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u5b9a\u4e49\u4e0d\u5b9a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u53c2\u6570\u7c7b\u578b\u53ef\u4ee5\u4e3a\u4efb\u610f\u7c7b\u578b\uff0c\u4f7f\u7528interface\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"254\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/4.png\" alt=\"\" class=\"wp-image-18065\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/4.png 651w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/4-300x117.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/4-600x234.png 600w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"56\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/5-1.png\" alt=\"\" class=\"wp-image-18066\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/5-1.png 651w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/5-1-300x26.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/5-1-600x52.png 600w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5\u3001\u4f7f\u7528\u5177\u540d\u8fd4\u56de\u503c(\u5e26\u6709\u53d8\u91cf\u540d\u7684\u8fd4\u56de\u503c)\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u540d\u8fd4\u56de\u503c\u8ba9\u51fd\u6570\u80fd\u591f\u5728\u8fd4\u56de\u524d\u5c06\u503c\u8d4b\u503c\u7ed9\u5177\u540d\u53d8\u91cf\uff0c\u6709\u52a9\u4e8e\u63d0\u5347\u51fd\u6570\u7684\u53ef\u8bfb\u6027\uff0c\u8981\u4f7f\u7528\u5177\u540d\u8fd4\u56de\u503c\uff0c\u53ef\u5728\u51fd\u6570\u7b7e\u540d\u7684\u8fd4\u56de\u503c\u90e8\u5206\u6307\u5b9a\u53d8\u91cf\u540d\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"277\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/1-9.png\" alt=\"\" class=\"wp-image-3227\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/1-9.png 591w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/1-9-300x141.png 300w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4e0a\u56fe\u4e2d\u7684return\u540e\u6ca1\u6709\u52a0\u8fd4\u56de\u503c\uff0c\u56e0\u6b64\u79f0\u4e3a\u88f8return\u8bed\u53e5\uff0c\u5728\u4e0b\u65b9\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5c06\u6309\u7167\u58f0\u660e\u987a\u5e8f\u8fd4\u56de\u5177\u540d\u53d8\u91cf\uff0c\u8ddf\u8d4b\u503c\u7684\u5148\u540e\u987a\u5e8f\u65e0\u5173<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9012\u5f52\u51fd\u6570\u662f\u4e0d\u65ad\u8c03\u7528\u81ea\u5df1\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u51fd\u6570\uff0c\u8981\u5728\u51fd\u6570\u4e2d\u5b9e\u73b0\u9012\u5f52\uff0c\u53ef\u5c06\u8c03\u7528\u81ea\u5df1\u7684\u4ee3\u7801\u4f5c\u4e3a\u7ec8\u6b62\u8bed\u53e5\u4e2d\u7684\u8fd4\u56de\u503c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"313\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/2-6.png\" alt=\"\" class=\"wp-image-3230\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/2-6.png 534w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/2-6-300x176.png 300w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\u7684test\u51fd\u6570\u6700\u540e\u4e00\u884c\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u800c\u662f\u8c03\u7528\u81ea\u5df1\uff0c\u518d\u6b21\u6267\u884c\u51fd\u6570\uff0c\u76f4\u5230a\u7684\u503c\u5927\u4e8e\u7b49\u4e8e5\uff0c\u624d\u7ec8\u6b62\u51fd\u6570\uff0c\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"531\" height=\"158\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/3-4.png\" alt=\"\" class=\"wp-image-3231\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/3-4.png 531w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/3-4-300x89.png 300w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>7\u3001\u5c06\u51fd\u6570\u4f5c\u4e3a\u503c\u8fdb\u884c\u4f20\u9012<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728go\u8bed\u8a00\u4e2d\uff0c\u53ef\u5c06\u51fd\u6570\u89c6\u4e3a\u4e00\u79cd\u7c7b\u578b\uff0c\u56e0\u6b64\u53ef\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5728\u901a\u8fc7\u53d8\u91cf\u6765\u8c03\u7528\u4ed6\u4eec\uff0c\u5982\u56fe:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"587\" height=\"277\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/3.png\" alt=\"\" class=\"wp-image-3587\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/3.png 587w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/3-300x142.png 300w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4e0a\u56fe\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u7c7b\u578b\u51fd\u6570\u8d4b\u503c\u7ed9fn\uff0c\u5c06fn\u4f20\u9012\u7ed9\u51fd\u6570test()\uff0c\u51fd\u6570test()\u7684\u53c2\u6570\u4e5f\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u7c7b\u578b\u4e5f\u4e3a\u5b57\u7b26\u4e32<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"144\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/1.png\" alt=\"\" class=\"wp-image-3583\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/1.png 525w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/1-300x82.png 300w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5b502\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"263\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/23-3.png\" alt=\"\" class=\"wp-image-3242\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/23-3.png 545w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/23-3-300x145.png 300w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"509\" height=\"173\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/2.png\" alt=\"\" class=\"wp-image-3585\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/2.png 509w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/2-300x102.png 300w\" sizes=\"auto, (max-width: 509px) 100vw, 509px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>8\u3001\u8c03\u7528\u51fd\u6570\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u5728\u5b9a\u4e49\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7684\u65b9\u5f0f\uff0c\u8ba9\u5f53\u524d\u4ee3\u7801\u8df3\u8f6c\u5230\u88ab\u8c03\u7528\u7684\u51fd\u6570\u4e2d\u8fdb\u884c\u6267\u884c\u3002\u8c03\u7528\u524d\u7684\u51fd\u6570\u5c40\u90e8\u53d8\u91cf\u90fd\u4f1a\u88ab\u4fdd\u5b58\u8d77\u6765\u4e0d\u4f1a\u4e22\u5931\uff1b\u88ab\u8c03\u7528\u7684\u51fd\u6570\u7ed3\u675f\u540e\uff0c\u6062\u590d\u5230\u88ab\u8c03\u7528\u51fd\u6570\u7684\u4e0b\u4e00\u884c\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u4e4b\u524d\u7684\u5c40\u90e8\u53d8\u91cf\u4e5f\u80fd\u7ee7\u7eed\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u53ea\u80fd\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\uff0c\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u540e\uff0c\u8fd9\u4e9b\u5c40\u90e8\u53d8\u91cf\u90fd\u4f1a\u88ab\u91ca\u653e\u5e76\u4e14\u5931\u6548\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7684\u51fd\u6570\u8c03\u7528\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd4\u56de\u503c\u53d8\u91cf\u5217\u8868\uff1d\u51fd\u6570\u540d\uff08\u53c2\u6570\u5217\u8868\uff09<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u51fd\u6570\u540d\uff1a \u9700\u8981\u8c03\u7528\u7684\u51fd\u6570\u540d\u3002<\/li>\n\n\n\n<li>\u53c2\u6570\u5217\u8868\uff1a \u53c2\u6570\u53d8\u91cf\u4ee5\u9017\u53f7\u5206\u9694\uff0c\u5c3e\u90e8\u65e0\u987b\u4ee5\u5206\u53f7\u7ed3\u5c3e\u3002<\/li>\n\n\n\n<li>\u8fd4\u56de\u503c\u53d8\u91cf\u5217\u8868\uff1a \u591a\u4e2a\u8fd4\u56de\u503c\u4f7f\u7528\u9017\u53f7\u5206\u9694\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1aresult := add(l , l)<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u533f\u540d\u51fd\u6570<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u652f\u6301\u533f\u540d\u51fd\u6570\uff0c\u5373\u5728\u9700\u8981\u4f7f\u7528\u51fd\u6570\u65f6\uff0c\u518d\u5b9a\u4e49\u51fd\u6570\uff0c\u533f\u540d\u51fd\u6570\u6ca1\u6709\u51fd\u6570\u540d\uff0c\u53ea\u6709\u51fd\u6570\u4f53\uff0c\u51fd\u6570\u53ef\u4ee5\u88ab\u4f5c\u4e3a\u4e00\u79cd\u7c7b\u578b\u88ab\u8d4b\u503c\u7ed9\u51fd\u6570\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u533b\u540d\u51fd\u6570\u4e5f\u5f80\u5f80\u4ee5\u53d8\u91cf\u65b9\u5f0f\u88ab\u4f20\u9012\u3002\u533f\u540d\u51fd\u6570\u7ecf\u5e38\u88ab\u7528\u4e8e\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\u3001\u95ed\u5305\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u533f\u540d\u51fd\u6570\u7684\u5b9a\u4e49\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func \uff08\u53c2\u6570\u5217\u8868\uff09\uff08\u8fd4\u56de\u53c2\u6570\u5217\u8868\uff09\uff5b\n\u51fd\u6570\u4f53\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u533f\u540d\u51fd\u6570\u7684\u5b9a\u4e49\u5c31\u662f\u6ca1\u6709\u540d\u5b57\u7684\u666e\u901a\u51fd\u6570\u5b9a\u4e49<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1<strong>\u3001\u533f\u540d\u51fd\u6570\u53ef\u4ee5\u5728\u58f0\u660e\u540e\u8c03\u7528\uff0c\u6b64\u65f6\u51fd\u6570\u53ea\u80fd\u6267\u884c\u4e00\u6b21\uff0c\u5982\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"540\" height=\"210\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/24-3.png\" alt=\"\" class=\"wp-image-3245\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/24-3.png 540w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/02\/24-3-300x117.png 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u533f\u540d\u51fd\u6570func()\u8981\u5199\u5728\u4e3b\u51fd\u6570main()\u4e2d\uff0c\u5426\u5219\u4f1a\u62a5\u9519<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2<strong>\u3001\u5c06\u533f\u540d\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u6b64\u65f6\u7684\u533f\u540d\u51fd\u6570\u5c31\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u4e86\uff0c\u5982\u56fe\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"271\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/6-1.png\" alt=\"\" class=\"wp-image-18070\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/6-1.png 652w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/6-1-300x125.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/04\/6-1-600x249.png 600w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u533f\u540d\u51fd\u6570\u7684\u7528\u9014\u975e\u5e38\u5e7f\u6cdb\uff0c\u533f\u540d\u51fd\u6570\u672c\u8eab\u662f\u4e00\u79cd\u503c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4fdd\u5b58\u5728\u5404\u79cd\u5bb9\u5668\u4e2d\u5b9e\u73b0\u56de\u8c03\u51fd\u6570\u548c\u64cd\u4f5c\u5c01\u88c5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001\u533b\u540d\u51fd\u6570\u7528\u4f5c\u56de\u8c03\u51fd\u6570<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4ee3\u7801\u5b9e\u73b0\u5bf9\u5207\u7247\u7684\u904d\u5386\u64cd\u4f5c\uff0c\u904d\u5386\u4e2d\u8bbf\u95ee\u6bcf\u4e2a\u5143\u7d20\u7684\u64cd\u4f5c\u4f7f\u7528\u533f\u540d\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u7528\u6237\u4f20\u5165\u4e0d\u540c\u7684\u533f\u540d\u51fd\u6570\u4f53\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5143\u7d20\u4e0d\u540c\u7684\u904d\u5386\u64cd\u4f5c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"328\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/5-8.png\" alt=\"\" class=\"wp-image-6631\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/5-8.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/5-8-300x139.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/5-8-600x279.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"164\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/6-8.png\" alt=\"\" class=\"wp-image-6632\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/6-8.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/6-8-300x70.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/6-8-600x139.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u51fd\u6570\u7684\u53c2\u6570\u4e3a\u533f\u540d\u51fd\u6570\u65f6\uff0c\u6b64\u65f6\u7684\u533f\u540d\u51fd\u6570\u4e0d\u9700\u8981\u5199{}\uff0c\u6bd4\u5982\u4e0a\u9762\u7684f func(int) \u5373\u53ef<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4<strong>\u3001\u4f7f\u7528\u533f\u540d\u51fd\u6570\u5b9e\u73b0\u64cd\u4f5c\u5c01\u88c5<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">go\u8bed\u8a00\u63d0\u4f9b\u7684flag\u5305\u53ef\u4ee5\u89e3\u6790\u547d\u4ee4\u884c\u7684\u53c2\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u5c06\u533f\u540d\u51fd\u6570\u4f5c\u4e3amap\u7684\u503c\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u52a8\u6001\u8c03\u7528\u533f\u540d\u51fd\u6570\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"509\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/7-7.png\" alt=\"\" class=\"wp-image-6636\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/7-7.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/7-7-300x216.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/7-7-600x433.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c8\u884c\uff0c\u58f0\u660e\u53d8\u91cfskillParam\uff0c\u901a\u8fc7flag\u5305\u89e3\u6790\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3askill\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u9ed8\u8ba4\u503c\uff0c\u7b2c\u4e09\u4e2a\u4e3a\u8bf4\u660e<\/li>\n\n\n\n<li>\u7b2c11\u884c\uff0cflag\u7684\u5305\u7684\u56fa\u5b9a\u8bed\u6cd5\uff0c\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u89e3\u6790\u5b8c\u6210\u540e\uff0cskillParam\u6307\u9488\u53d8\u91cf\u5c06\u6307\u5411\u547d\u4ee4\u884c\u4f20\u5165\u7684\u503c<\/li>\n\n\n\n<li>\u7b2c12\u884c\uff0c\u5b9a\u4e49\u4e00\u4e2amap\uff0c\u952e\u7c7b\u578b\u4e3a\u5b57\u7b26\u4e32\uff0c\u503c\u7c7b\u578b\u4e3a\u533f\u540d\u51fd\u6570func()<\/li>\n\n\n\n<li>\u7b2c13-18\u884c\uff0c\u901a\u8fc7\u58f0\u660e\u5f0f\u586b\u5145\u65b9\u5f0f\u521d\u59cb\u5316\u952e\u503c\u5bf9\uff0c\u503c\u4e5f\u662f\u533f\u540d\u51fd\u6570\u7c7b\u578b<\/li>\n\n\n\n<li>\u7b2c20-23\u884c\uff0c\u5224\u65ad\u662f\u5426\u5b58\u5728\u8fd9\u4e2a\u952e\uff0c\u5982\u679c\u6709\u90a3\u4e48\u5c31\u8c03\u7528\uff0c\u5426\u5219\u6253\u5370\u6ca1\u6709\u53d1\u73b0\uff0c\u6ce8\u610f\uff0cf \u8868\u793a\u503c\uff0c\u5982\u679c\u5b58\u5728\u8fd9\u4e2a\u952e\uff0c\u90a3\u4e48\u5c31f\u5c31\u662f\u8fd9\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u4e5f\u5c31\u662f\u5bf9\u5e94\u7684\u533f\u540d\u51fd\u6570\uff0c\u6b64\u65f6\u901a\u8fc7f()\u5373\u53ef\u8c03\u7528\u6b64\u51fd\u6570<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u547d\u4ee4\u884c\u6267\u884c\u547d\u4ee4\uff0c\u5c06\u53c2\u6570\u4f20\u9012\u7ed9skill\uff0c\u6253\u5370\u7ed3\u679c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"113\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/8-6.png\" alt=\"\" class=\"wp-image-6639\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/8-6.png 705w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/8-6-300x48.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/8-6-600x96.png 600w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u95ed\u5305&#8211;\u5f15\u7528\u4e86\u5916\u90e8\u53d8\u91cf\u7684\u533f\u540d\u51fd\u6570<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u95ed\u5305\u662f\u5f15\u7528\u4e86\u81ea\u7531\u53d8\u91cf\u7684\u51fd\u6570\uff0c\u88ab\u5f15\u7528\u7684\u81ea\u7531\u53d8\u91cf\u548c\u51fd\u6570\u4e00\u540c\u5b58\u5728\uff0c\u5373\u4f7f\u5df1\u7ecf\u79bb\u5f00\u4e86\u81ea\u7531\u53d8\u91cf\u7684\u73af\u5883\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u6216\u8005\u5220\u9664\uff0c\u5728\u95ed\u5305\u4e2d\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528\u8fd9\u4e2a\u81ea\u7531\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u7b80\u5355\u7684\u8bf4\uff1a\u51fd\u6570\uff0b\u5f15\u7528\u73af\u5883\uff1d\u95ed\u5305<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u4e00\u4e2a\u51fd\u6570\u4e0e\u4e0d\u540c\u5f15\u7528\u73af\u5883\u7ec4\u5408\uff0c\u53ef\u4ee5\u5f62\u6210\u4e0d\u540c\u7684\u5b9e\u4f8b\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"280\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/9-6.png\" alt=\"\" class=\"wp-image-6642\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/9-6.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/9-6-300x119.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/9-6-600x238.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u51fd\u6570\u7c7b\u578b\u5c31\u50cf\u7ed3\u6784\u4f53\u4e00\u6837\uff0c\u53ef\u4ee5\u88ab\u5b9e\u4f8b\u5316\u3002\u51fd\u6570\u672c\u8eab\u4e0d\u5b58\u50a8\u4efb\u4f55\u4fe1\u606f\uff0c\u53ea\u6709\u4e0e\u5f15\u7528\u73af\u5883\u7ed3\u5408\u540e\u5f62\u6210\u7684\u95ed\u5305\u624d\u5177\u6709\u201c\u8bb0\u5fc6\u6027\u201d\uff0c\u51fd\u6570\u662f\u7f16\u8bd1\u671f\u9759\u6001\u7684\u6982\u5ff5\uff0c\u800c\u95ed\u5305\u662f\u8fd0\u884c\u671f\u52a8\u6001\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u5728\u95ed\u5305\u5185\u90e8\u4fee\u6539\u5f15\u7528\u7684\u53d8\u91cf\uff1a<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u95ed\u5305\u5bf9\u5b83\u4f5c\u7528\u57df\u4e0a\u90e8\u53d8\u91cf\u7684<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5f15\u7528<\/mark><\/strong>\u53ef\u4ee5\u8fdb\u884c\u4fee\u6539\uff0c\u4fee\u6539\u5f15\u7528\u7684\u53d8\u91cf\u5c31\u4f1a\u5bf9\u53d8\u91cf\u8fdb\u884c\u5b9e\u9645\u4fee\u6539\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"279\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-5.png\" alt=\"\" class=\"wp-image-6645\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-5.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-5-300x119.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-5-600x237.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"121\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-6.png\" alt=\"\" class=\"wp-image-6647\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-6.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-6-300x51.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-6-600x103.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0c\u53d8\u91cfarr1\u7684\u503c\u5df2\u7ecf\u88ab\u4fee\u6539\uff0c\u5728\u533f\u540d\u51fd\u6570\u5916\u90e8\u8bbf\u95ee\u4f9d\u65e7\u662f\u4fee\u6539\u4e4b\u540e\u7684\u503c<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u95ed\u5305\u5bf9\u6355\u83b7\u7684\u5916\u90e8\u53d8\u91cf\u4e0d\u662f\u901a\u8fc7\u4f20\u503c\u65b9\u5f0f\u8bbf\u95ee\uff0c\u800c\u662f\u901a\u8fc7\u5f15\u7528\u7684\u65b9\u5f0f\u8bbf\u95ee<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">2<strong>\u3001\u95ed\u5305\u7684\u8bb0\u5fc6\u6548<\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u5e94\uff1a<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u88ab\u6355\u83b7\u5230\u95ed\u5305\u4e2d\u7684\u53d8\u91cf\u8ba9\u95ed\u5305\u672c\u8eab\u62e5\u6709\u4e86\u8bb0\u5fc6\u6548\u5e94\uff0c\u95ed\u5305\u4e2d\u7684\u903b\u8f91\u53ef\u4ee5\u4fee\u6539\u95ed\u5305\u6355\u83b7\u7684\u53d8\u91cf\uff0c \u53d8\u91cf\u4f1a\u8ddf\u968f\u95ed\u5305\u751f\u547d\u671f\u4e00\u76f4\u5b58\u5728\uff0c\u95ed\u5305\u672c\u8eab\u5c31\u5982\u540c\u53d8\u91cf\u4e00\u6837\u62e5\u6709\u4e86\u8bb0\u5fc6\u6548\u5e94\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\uff0c\u901a\u8fc7\u51fd\u6570\u4e0e\u95ed\u5305\uff0c\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7d2f\u52a0\u5668\u7684\u529f\u80fd\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"441\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/13-6.png\" alt=\"\" class=\"wp-image-6651\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/13-6.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/13-6-300x187.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/13-6-600x375.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c5\u884c\uff1a\u5b9a\u4e49\u4e00\u4e2a\u7d2f\u52a0\u5668\u751f\u6210\u51fd\u6570\uff0c\u8fd4\u56de\u503c\u4e3a\u5e26\u53d8\u91cf\u7684int\u7c7b\u578b\uff0c\u53d8\u91cf\u540d\u4e3a\u533f\u540d\u51fd\u6570<\/li>\n\n\n\n<li>\u7b2c6\u884c\uff1a\u8fd4\u56de\u4e00\u4e2a\u95ed\u5305\u51fd\u6570<\/li>\n\n\n\n<li>\u7b2c7\u884c\uff1a\u5bf9\u5f15\u7528Accumulate\u53c2\u6570\u53d8\u91cf\u8fdb\u884c\u7d2f\u52a0\uff0c\u7d2f\u8ba1\u540e\u7684\u503c\u901a\u8fc7\u95ed\u5305\u7684\u8fd4\u56de\u503c\u8fd4\u56de<\/li>\n\n\n\n<li>\u7b2c12\u884c\uff1a\u521b\u5efa\u4e00\u4e2a\u7d2f\u52a0\u5668\uff0c\u521d\u59cb\u503c\u4e3a1\uff0c\u8fd4\u56de\u7684accumulate\u662f\u7c7b\u578b\u4e3afunc()int\u7684\u51fd\u6570\u53d8\u91cf<\/li>\n\n\n\n<li>\u7b2c13\u884c\uff1a\u8c03\u7528accumulate()\uff0c\u4ece\u7b2c6\u884c\u5230\u7b2c9\u884c\u5f00\u59cb\u6267\u884c\u533f\u540d\u51fd\u6570\u903b\u8f91<\/li>\n\n\n\n<li>\u7b2c15\u884c\uff1a\u6253\u5370\u51fd\u6570\u7684\u5730\u5740<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"164\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-6.png\" alt=\"\" class=\"wp-image-6657\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-6.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-6-300x70.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-6-600x139.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff1a\u7b2c\u4e00\u6b21\u7684\u6253\u5370\u7ed3\u679c\u4e3a2\uff0c\u56e0\u4e3a\u521d\u59cb\u503c\u4e3a1\uff0c\u6267\u884c\u7b2c\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u4fee\u6539\u4e86value\u7684\u503c\uff0c\u8fd4\u56de\u4e3a2\uff0c\u5f53\u7b2c\u4e8c\u6b21\u8c03\u7528\u7684\u65f6\u5019\uff0c\u95ed\u5305\u51fd\u6570\u4f9d\u65e7\u8bb0\u5f97value\u7684\u503c\u4e3a2\uff0c\u518d\u6b21\u7d2f\u52a0\uff0c\u56e0\u6b64\u8fd4\u56de\u7684\u503c\u4e3a3\uff0c\u6bcf\u8c03\u7528\u4e00\u6b21accumulator \u90fd\u4f1a\u81ea\u52a8\u5bf9\u5f15\u7528\u7684\u53d8\u91cf\u8fdb\u884c\u7d2f\u52a0\uff0c\u4e0b\u9762\u7684\u8c03\u7528\u539f\u7406\u76f8\u540c\uff0c\u6700\u540e\u770b\u4e24\u6b21\u6253\u5370\u7684\u51fd\u6570\u5730\u5740\u4e0d\u540c\uff0c\u56e0\u4e3a\u4ed6\u4eec\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u95ed\u5305\u5b9e\u4f8b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>3\u3001\u95ed\u5305\u7ecf\u5e38\u51fa\u73b0\u7684\u5171\u4eab\u5faa\u73af\u53d8\u91cf<\/strong><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u95ee\u9898<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u95ed\u5305\u4e2d\u83b7\u53d6\u5faa\u73af\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6253\u5370\u7684\u7ed3\u679c\u53ef\u80fd\u4e0d\u53ca\u9884\u671f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"358\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/9-3.jpg\" alt=\"\" class=\"wp-image-16357\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/9-3.jpg 695w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/9-3-300x155.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/9-3-600x309.jpg 600w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u9884\u671f\u7ed3\u679c\u5e94\u8be5\u4e3aa-g\uff0c\u5b9e\u9645\u6253\u5370\u7ed3\u679c\u4e3a:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"191\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/4-4.jpg\" alt=\"\" class=\"wp-image-16358\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/4-4.jpg 678w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/4-4-300x85.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/4-4-600x169.jpg 600w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0c\u7ed3\u679c\u4e0d\u53ca\u9884\u671f\uff0c\u56e0\u4e3amain\u6240\u5728\u7684goroutinue\u548cgo func()\u7684goroutinue\u4e24\u4e2a\u662f\u65e0\u5e8f\u8fdb\u884c\u7684\uff0c\u5f53\u6267\u884c\u95ed\u5305\u51fd\u6570\u7684\u65f6\u5019\uff0cv\u7684\u503c\u53ef\u80fd\u5df2\u7ecf\u662f\u6700\u540e\u4e00\u4e2a\uff0c\u4e5f\u53ef\u80fd\u662f\u968f\u673a\uff0c\u56e0\u6b64\u95ed\u5305\u51fd\u6570\u53ef\u80fd\u6bcf\u6b21\u83b7\u53d6\u7684\u90fd\u662f\u540c\u4e00\u4e2av\u7684\u503c\uff0c\u8981\u60f3\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ea\u9700\u8981\u5c06\u5faa\u73af\u53d8\u91cf\u7684\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u65b0\u7684\u503c\uff0c\u6bcf\u6b21\u5faa\u73af\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u503c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"383\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/1-4.jpg\" alt=\"\" class=\"wp-image-16359\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/1-4.jpg 693w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/1-4-300x166.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/1-4-600x332.jpg 600w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"194\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/2-5.jpg\" alt=\"\" class=\"wp-image-16360\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/2-5.jpg 695w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/2-5-300x84.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/2-5-600x167.jpg 600w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u5ef6\u8fdf\u6267\u884c\u8bed\u53e5\uff08defer)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7684defer \u8bed\u53e5\u4f1a\u5c06\u5176\u540e\u9762\u8ddf\u968f\u7684\u8bed\u53e5\u8fdb\u884c\u5ef6\u8fdf\u5904\u7406\u3002\u5728defer \u5f52\u5c5e\u7684\u51fd\u6570\u5373\u5c06\u8fd4\u56de\u65f6\uff0c\u5c06\u5ef6\u8fdf\u5904\u7406\u7684\u8bed\u53e5\u6309defer \u7684\u9006\u5e8f\u8fdb\u884c\u6267\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5148\u88abdefer \u7684\u8bed\u53e5\u6700\u540e\u88ab\u6267\u884c\uff0c\u6700\u540e\u88abdefer \u7684\u8bed\u65ec\uff0c\u6700\u5148\u88ab\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1\u3001\u591a\u4e2a\u5ef6\u8fdf\u6267\u884c\u8bed\u65ec\u7684\u5904\u7406\u987a\u5e8f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u5c06\u4e00\u7cfb\u5217\u7684\u6570\u503c\u8fdb\u884c\u6253\u5370\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"258\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/10-6.png\" alt=\"\" class=\"wp-image-6668\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/10-6.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/10-6-300x110.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/10-6-600x219.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"162\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-6.png\" alt=\"\" class=\"wp-image-6669\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-6.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-6-300x69.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-6-600x137.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0c\u4ee3\u7801\u7684\u5ef6\u8fdf\u987a\u5e8f\u4e0e\u6700\u7ec8\u7684\u6267\u884c\u987a\u5e8f\u662f\u53cd\u5411\u7684\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u5ef6\u8fdf\u8c03\u7528\u662f\u5728defer \u6240\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u8fdb\u884c<\/mark><\/strong>\uff0c\u51fd\u6570\u7ed3\u675f\u53ef\u4ee5\u662f\u6b63\u5e38\u8fd4\u56de\u65f6\uff0c\u4e5f\u53ef\u4ee5\u662f\u53d1\u751f\u5bbf\u673a\u65f6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2<strong>\u3001\u4f7f\u7528\u5ef6\u8fdf\u6267\u884c\u8bed\u53e5\u5728\u51fd\u6570\u9000\u51fa\u65f6\u91ca\u653e\u8d44\u6e90<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5904\u7406\u4e1a\u52a1\u6216\u903b\u8f91\u4e2d\u6d89\u53ca\u6210\u5bf9\u7684\u64cd\u4f5c\u662f\u4e00\u4ef6\u6bd4\u8f83\u70e6\u7410\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u6253\u5f00\u548c\u5173\u95ed\u6587\u4ef6\u3001\u63a5\u6536\u8bf7\u6c42\u548c\u56de\u590d\u8bf7\u6c42\u3001\u52a0\u9501\u548c\u89e3\u9501\u7b49\u3002\u5728\u8fd9\u4e9b\u64cd\u4f5c\u4e2d\uff0c \u6700\u5bb9\u6613\u5ffd\u7565\u7684\u5c31\u662f\u5728\u6bcf\u4e2a\u51fd\u6570\u9000\u51fa\u5904\u6b63\u786e\u5730\u91ca\u653e\u548c\u5173\u95ed\u8d44\u6e90\u3002defer \u8bed\u53e5\u6b63\u597d\u662f\u5728\u51fd\u6570\u9000\u51fa\u65f6\u6267\u884c\u7684\u8bed\u65ec\uff0c\u6240\u4ee5\u4f7f\u7528defer \u80fd\u975e\u5e38\u65b9\u4fbf\u5730\u5904\u7406\u8d44\u6e90\u91ca<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(<strong>1) \u4f7f\u7528\u5ef6\u8fdf\u4e95\u53d1\u89e3\u9501<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u5728\u51fd\u6570\u4e2d\u5e76\u53d1\u4f7f\u7528map \uff0c\u4e3a\u9632\u6b62\u7ade\u6001\u95ee\u9898\uff0c\u4f7f\u7528sync.Mutex \u8fdb\u884c\u52a0\u9501\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"346\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-7.png\" alt=\"\" class=\"wp-image-6675\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-7.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-7-300x147.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/12-7-600x294.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c9\u884c\uff1a\u5b9e\u4f8b\u5316\u4e00\u4e2amap\uff0c\u952e\u662fstring\u7c7b\u578b\uff0c\u503c\u662fint<\/li>\n\n\n\n<li>\u7b2c10\u884c\uff1amap \u9ed8\u8ba4\u4e0d\u662f\u5e76\u53d1\u5b89\u5168\u7684\uff0c\u51c6\u5907\u4e00\u4e2async.Mutex \u4e92\u65a5\u91cf\u4fdd\u62a4map\u7684\u8bbf\u95ee<\/li>\n\n\n\n<li>\u7b2c13\u884c\uff1areadValue \uff08\uff09 \u51fd\u6570\u7ed9\u5b9a\u4e00\u4e2a\u952e\uff0c\u4ecemap \u4e2d\u83b7\u5f97\u503c\u540e\u8fd4\u56de\uff0c\u8be5\u51fd\u6570\u4f1a\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u4f7f\u7528\uff0c\u9700\u8981\u4fdd\u8bc1\u5e76\u53d1\u5b89\u5168<\/li>\n\n\n\n<li>\u7b2c14\u884c\uff1a\u4f7f\u7528\u4e92\u65a5\u91cf\u52a0\u9501<\/li>\n\n\n\n<li>\u7b2c15\u884c\uff1a\u4f7f\u7528defer \u8bed\u53e5\u6dfb\u52a0\u4e92\u65a5\u91cf\u89e3\u9501\uff0c\u8be5\u8bed\u53e5\u4e0d\u4f1a\u9a6c\u4e0a\u6267\u884c\uff0c\u800c\u662f\u7b49readValue()\u8fd4\u56de\u65f6\u624d\u4f1a\u88ab\u6267\u884c<\/li>\n\n\n\n<li>\u7b2c16\u884c\uff1a\u4ecemap\u4e2d\u67e5\u8be2\u503c\u5e76\u8fd4\u56de<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">(2)\u3001\u5ef6\u8fdf\u8bed\u53e5\u914d\u7f6ereturn\u8fd4\u56de\u7684\u4f8b\u5b50\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"318\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-4.png\" alt=\"\" class=\"wp-image-14786\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-4.png 698w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-4-300x137.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/3-4-600x273.png 600w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"692\" height=\"125\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-5.png\" alt=\"\" class=\"wp-image-14787\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-5.png 692w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-5-300x54.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/4-5-600x108.png 600w\" sizes=\"auto, (max-width: 692px) 100vw, 692px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u770b\u4e00\u4e2a\u5b9a\u4e49\u547d\u540d\u8fd4\u56de\u503c\u5e76\u4fee\u6539\u6570\u636e\u7684\u4f8b\u5b50\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"389\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/2-6.png\" alt=\"\" class=\"wp-image-18918\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/2-6.png 651w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/2-6-300x179.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/2-6-600x359.png 600w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"93\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/3-5.png\" alt=\"\" class=\"wp-image-18919\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/3-5.png 651w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/3-5-300x43.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/3-5-600x86.png 600w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\u7684\u7ed3\u679c\u8fd4\u56de11\u800c\u4e0d\u662f10 \uff0c\u662f\u56e0\u4e3aGo \u8bed\u8a00\u7684 \u200b<strong>\u200b&#8221;\u547d\u540d\u8fd4\u56de\u503c + defer \u4fee\u6539&#8221;\u200b<\/strong>\u200b \u673a\u5236\u5728\u8d77\u4f5c\u7528\uff0c\u547d\u540d\u8fd4\u56de\u503c\u7684\u610f\u601d\u5c31\u662f\u5728\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4f4d\u7f6e\u4e5f\u6307\u5b9a\u4e86\u540d\u5b57(\u4e0a\u56fe\u4e2d\u7684result int)\uff0creturn result\u5b9e\u9645\u662f\u5c06result\u653e\u5165\u8fd4\u56de\u6808\u4e2d\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u5982\u679c\u662f\u547d\u540d\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u6b64\u65f6\u6808\u5185\u4e3aresult\u7684\u5f15\u7528<\/mark><\/strong>\uff0cdefer\u4ecd\u7136\u53ef\u4ee5\u4fee\u6539\u6b64\u5f15\u7528\u7684\u503c\uff0c\u4fee\u6539\u540e\u6700\u540e\u4e00\u8d77\u8fd4\u56de<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u662f\u533f\u540d\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u7ed3\u679c\u5c31\u4e0d\u4e00\u6837\u4e86\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"399\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/5-4.png\" alt=\"\" class=\"wp-image-18925\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/5-4.png 650w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/5-4-300x184.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/5-4-600x368.png 600w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"99\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/6-4.png\" alt=\"\" class=\"wp-image-18926\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/6-4.png 650w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/6-4-300x46.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/08\/6-4-600x91.png 600w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0cdefer\u4fee\u6539\u7684\u503c\u4e3a11\uff0c\u4f46\u662freturn\u6700\u7ec8\u8fd4\u56de\u7684\u503c\u4e3a10\uff0c<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u56e0\u4e3a\u533f\u540d\u8fd4\u56de\u503c\u5728return\u7684\u65f6\u5019\u4f20\u9012\u7684\u662f\u503c\u7684\u526f\u672c\uff0c\u56e0\u6b64defer\u4fee\u6539\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u6765\u7684\u503c<\/mark><\/strong><\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1adefer\u662f\u5728return\u8bed\u53e5\u6267\u884c\u5b8c\u6210\u540e\u5e76\u4e14\u5728\u51fd\u6570\u771f\u6b63\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\u524d\u6267\u884c\uff0c\u56e0\u6b64\u5148\u6253\u5370defer\uff0c\u7136\u540e\u6253\u5370return\u8fd4\u56de\u5185\u5bb9<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u4e8e\u4e92\u65a5\u9501\u7684\u5185\u5bb9\u53ef\u53c2\u8003\u94fe\u63a5\uff1a<a href=\"https:\/\/blog.ywdevops.cn\/index.php\/2022\/01\/29\/mutex\/\">https:\/\/blog.ywdevops.cn\/index.php\/2022\/01\/29\/mutex\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2\u3001\u4f7f\u7528\u5ef6\u8fdf\u91ca\u653e\u6587\u4ef6\u65ec\u67c4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u4ef6\u7684\u64cd\u4f5c\u9700\u8981\u7ecf\u8fc7\u6253\u5f00\u6587\u4ef6\u3001\u83b7\u53d6\u548c\u64cd\u4f5c\u6587\u4ef6\u8d44\u6e90\u3001\u5173\u95ed\u8d44\u6e90\u51e0\u4e2a\u8fc7\u7a0b\uff0c\u5982\u679c\u5728\u64cd\u4f5c\u5b8c\u6bd5\u540e\u4e0d\u5173\u95ed\u6587\u4ef6\u8d44\u6e90\uff0c\u8fdb\u7a0b\u5c06\u4e00\u76f4\u65e0\u6cd5\u91ca\u653e\u6587\u4ef6\u8d44\u6e90\uff0c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u5c06\u6839\u636e\u6587\u4ef6\u540d\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\uff0c\u51fd\u6570\u4e2d\u9700\u8981\u6253\u5f00\u6587\u4ef6\u3001\u83b7\u53d6\u5927\u5c0f\u4e0e\u5173\u95ed\u6587\u4ef6\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"502\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-7.png\" alt=\"\" class=\"wp-image-6688\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-7.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-7-300x213.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-7-600x427.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c8\u884c\uff1a\u5b9a\u4e49\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u503c\u662f64 \u4f4d\u7684\u6587\u4ef6\u5927\u5c0f\u503c<\/li>\n\n\n\n<li>\u7b2c9\u884c\uff1a\u4f7f\u7528OS \u5305\u63d0\u4f9b\u7684\u51fd\u6570Open(),\u6839\u636e\u7ed9\u5b9a\u7684\u6587\u4ef6\u540d\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u64cd\u4f5c\u6587\u4ef6\u7528\u7684\u53e5\u67c4\u548c\u64cd\u4f5c\u9519\u8bef<\/li>\n\n\n\n<li>\u7b2c10\u884c\uff1a\u5982\u679c\u6253\u5f00\u7684\u8fc7\u7a0b\u4e2d\u53d1\u751f\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u6ca1\u627e\u5230\u3001\u6587\u4ef6\u88ab\u5360\u7528\u7b49\uff0c\u5c06\u8fd4\u56de\u6587\u4ef6\u5927\u5c0f\u4e3a0<\/li>\n\n\n\n<li>\u7b2c14\u884c\uff1a\u83b7\u53d6\u6587\u4ef6\u7684\u72b6\u6001\u4fe1\u606f<\/li>\n\n\n\n<li>\u7b2c15\u884c\uff1a\u5982\u679c\u6587\u4ef6\u72b6\u6001\u4fe1\u606f\u6709\u9519\u8bef\uff0c\u5c06\u8fd4\u56de0<\/li>\n\n\n\n<li>\u7b2c18\u884c\uff1a\u6839\u636e\u6587\u4ef6\u72b6\u6001\u4fe1\u606f\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u4fe1\u606f<\/li>\n\n\n\n<li>\u7b2c19\u884c\uff1a\u8fd4\u56de\u6587\u4ef6\u7684\u5927\u5c0f<\/li>\n\n\n\n<li>\u7b2c13\u884c\uff1a\u5728\u6587\u4ef6\u6b63\u5e38\u6253\u5f00\u540e\uff0c\u4f7f\u7528defer\u8bed\u53e5\uff0c\u5c06f.close()\u5ef6\u8fdf\u8c03\u7528\uff0c\u6ce8\u610f\uff1a\u4e0d\u80fd\u5c06\u8fd9\u4e00\u6837\u4ee3\u7801\u653e\u5728\u7b2c9\u884c\u4e4b\u540e\uff0c\u56e0\u4e3a\u4e00\u65e6\u6587\u4ef6\u6253\u5f00\u5931\u8d25\uff0cf \u5c06\u4e3a\u7a7a\uff0c\u6bd4\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u6b64\u65f6\u5728\u6267\u884cdefer\u6765\u5173\u95ed\u6587\u4ef6\u65f6\uff0c\u5c06\u89e6\u53d1\u5b95\u673a\u9519\u8bef<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u963b\u585e\u51fd\u6570\uff1a\u5f53\u8fd9\u4e2a\u51fd\u6570\u4e0d\u6267\u884c\u5b8c\uff0c\u51fd\u6570\u6240\u5728\u7ebf\u7a0b\u5c31\u4e00\u76f4\u505c\u6b62\u5728\u8fd9\u91cc\u4e0d\u52a8\u3002\u610f\u601d\u5c31\u662f\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4e00\u76f4\u7b49\u5f85\uff0c\u53ea\u6709\u5728\u5f97\u5230\u7ed3\u679c\u4e4b\u540e\u624d\u4f1a\u8fd4\u56de\u3002\u540c\u65f6\u5b83\u4e5f\u4e0d\u5141\u8bb8\u7a0b\u5e8f\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6253\u5370\u7ed3\u679c\u8868\u793a\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"95\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-6.png\" alt=\"\" class=\"wp-image-6699\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-6.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-6-300x40.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/2-6-600x81.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001\u5f53\u9047\u5230panic\u7684\u65f6\u5019\uff0cdefer\u8fd8\u662f\u4f1a\u6267\u884c<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"277\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/5-4.png\" alt=\"\" class=\"wp-image-14793\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/5-4.png 700w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/5-4-300x119.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/5-4-600x237.png 600w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"208\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/6-3.png\" alt=\"\" class=\"wp-image-14794\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/6-3.png 698w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/6-3-300x89.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/09\/6-3-600x179.png 600w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1adefer\u53ea\u80fd\u7528\u5728\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u524d\u9762\uff0c\u4e0d\u80fd\u7528\u5728\u5176\u4ed6\u524d\u9762\uff0c\u4f8b\u5982for \u3001if \u7b49<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001defer\u8bed\u53e5\u867d\u7136\u5728\u51fd\u6570\u7ed3\u675f\u7684\u65f6\u5019\u6267\u884c\uff0c\u4f46\u662f\u5982\u679c\u5ef6\u8fdf\u6267\u884c\u51fd\u6570\u4e2d\u6709\u53c2\u6570\uff0c\u90a3\u4e48\u5148\u8ba1\u7b97\u53c2\u6570\u503c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"508\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5.png\" alt=\"\" class=\"wp-image-18078\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5.png 651w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5-300x234.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5-600x468.png 600w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"165\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6.png\" alt=\"\" class=\"wp-image-18079\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6.png 653w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6-300x76.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6-600x152.png 600w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\u7684\u4e09\u4e2a\u5ef6\u8fdf\u51fd\u6570,\u4e00\u4e2afirst()\uff0c\u8fd8\u6709\u4e24\u4e2aPrintln()\uff0c\u90fd\u6709\u53c2\u6570\uff0c\u4f46\u662f\u7b2c\u4e00\u4e2a\u53c2\u6570num\u5c31\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5355\u72ec\u7684\u53d8\u91cf\u4e0d\u80fd\u6267\u884c\u4efb\u4f55\u8ba1\u7b97\uff0c\u540e\u9762\u7684\u4e24\u4e2afmt.Println()\u4e2d\u7684\u53c2\u6570second\u548cthird\u53ef\u8fdb\u884c\u8ba1\u7b97\uff0c\u56e0\u6b64\u5148\u6267\u884csecond\u7136\u540e\u6267\u884cthird\uff0c\u5728\u6267\u884c\u5b8cEnd\u540e\uff0c\u5f00\u59cb\u5012\u7740\u6765\u6253\u5370defer\u7684\u51fd\u6570\uff0cthird\u8fd9\u91cc\u6253\u5370\u7684\u662fthird\u51fd\u6570\u7684return\uff0csecond\u6253\u5370\u7684\u662fsecond\u51fd\u6570\u7684return\uff0cfirst\u56e0\u4e3a\u53ea\u4f20\u9012\u4e86num\u53c2\u6570\uff0c\u56e0\u6b64\uff0c\u53ea\u6253\u5370first\u4e2d\u7684fmt\uff0c\u4e0d\u6253\u5370return\uff0c\u5982\u679c\u5c06defer first(num)\u6539\u4e3adefer fmt.Println(first(num))\u5c31\u4f1a\u6253\u5370return\u7684\u503c\u4e86\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">5\u3001\u5728\u51fd\u6570\u4e2d\u4f7f\u7528defer\u8bed\u53e5\u5ef6\u8fdf\u8c03\u7528\u533f\u540d\u51fd\u6570\u65f6\uff0c\u5982\u679c\u8be5\u51fd\u6570\u6709\u8fd4\u56de\u503c\uff0c\u5219\u53ef\u80fd\u6709\u4e24\u79cd\u60c5\u51b5\uff0c\u4e00\u79cd\u662f\u8fd4\u56de\u503c\u672a\u8bbe\u7f6e\u53d8\u91cf\u540d\uff0c\u53e6\u4e00\u79cd\u662f\u4e3a\u8fd4\u56de\u503c\u8bbe\u7f6e\u53d8\u91cf\u540d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u3001test\u51fd\u6570\u8fd4\u56de\u503c\u672a\u8bbe\u7f6e\u53d8\u91cf\u540d\uff0c\u8fd4\u56de\u7684\u662f\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684i\u7684\u503c\uff0c\u6b64\u65f6defer\u662f\u4e0d\u80fd\u4fee\u6539\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u56e0\u6b64\u8fd4\u56de\u503c\u8fd8\u662fi\u7684\u96f6\u503c\uff0c\u4e5f\u5c31\u662f0\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"410\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2.png\" alt=\"\" class=\"wp-image-18084\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2.png 648w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-300x190.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-600x380.png 600w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"89\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3.png\" alt=\"\" class=\"wp-image-18085\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3.png 648w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3-300x41.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3-600x82.png 600w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">(2)\u3001test\u51fd\u6570\u8fd4\u56de\u503c\u8bbe\u7f6e\u4e86\u53d8\u91cf\u540d,defer\u53ef\u4ee5\u4fee\u6539test\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u6700\u7ec8\u7684\u8fd4\u56de\u503c\u4e3a2\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"485\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1.png\" alt=\"\" class=\"wp-image-18087\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1.png 651w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1-300x224.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1-600x447.png 600w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"95\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-1.png\" alt=\"\" class=\"wp-image-18089\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-1.png 649w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-1-300x44.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-1-600x88.png 600w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u662f\u7ec4\u7ec7\u597d\u7684\u3001\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u3001\u7528\u6765\u5b9e\u73b0\u5355\u4e00\u6216\u76f8\u5173\u8054\u529f\u80fd\u7684\u4ee3\u7801\u6bb5\uff0c\u5176\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7684\u6a21\u5757\u6027\u548c\u4ee3\u7801\u7684\u91cd\u590d\u5229\u7528\u7387 Go [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3220,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[114],"class_list":["post-2895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong","tag-go"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2895"}],"version-history":[{"count":105,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2895\/revisions"}],"predecessor-version":[{"id":18928,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2895\/revisions\/18928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/3220"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}