{"id":2846,"date":"2021-01-09T00:28:20","date_gmt":"2021-01-08T16:28:20","guid":{"rendered":"http:\/\/blog.ywdevops.cn\/?p=2846"},"modified":"2024-03-11T09:19:27","modified_gmt":"2024-03-11T01:19:27","slug":"go-4","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2021\/01\/09\/go-4\/","title":{"rendered":"Go\u8bed\u8a00\u4e4b\u6d41\u7a0b\u63a7\u5236"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7684\u5e38\u7528\u6d41\u7a0b\u63a7\u5236\u6709if \u548cfor \uff0c\u800cswitch \u548cgoto \u4e3b\u8981\u662f\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\u3001\u964d\u4f4e\u91cd\u590d\u4ee3\u7801\u800c\u751f\u7684\u7ed3\u6784\uff0c\u5c5e\u4e8e\u6269\u5c55\u7c7b\u7684\u6d41\u7a0b\u63a7\u5236\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u6761\u4ef6\u5224\u65ad(if)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go \u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7if \u5173\u952e\u5b57\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if \u8868\u8fbe\u5f0f1 {\n\u5206\u652f1\n} else if \u8868\u8fbe\u5f0f2 {\n\u5206\u652f2\n} else{\n\u5206\u652f3\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u8868\u8fbe\u5f0f1\u4e3atrue\u65f6\uff0c\u6267\u884c\u5206\u652f1\u7684\u8bed\u53e5\uff0c\u5426\u5219\u5982\u679c\u8868\u8fbe\u5f0f2\u4e3atrue\uff0c\u5219\u6267\u884c\u5206\u652f2\uff0c\u5982\u679c\u90fd\u4e0d\u6ee1\u8db3\uff0c\u90a3\u4e48\u6267\u884c\u5206\u652f3<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">Go \u8bed\u8a00\u89c4\u5b9a\u4e0eif \u5339\u914d\u7684\u5de6\u62ec\u53f7&#8221;{&#8220;\u5fc5\u987b\u4e0eif \u548c\u8868\u8fbe\u5f0f\u653e\u5728\u540c\u4e00\u884c,\u5982\u679c\u653e\u5728\u5176\u4ed6\u4f4d\u7f6e\uff0c\u90a3\u4e48\u5c06\u4f1a\u51fa\u9519\u3002\u540c\u7406\uff0c\u4e0eelse \u5339\u914d\u7684 &#8220;{&#8221; \u4e5f\u5fc5\u987b\u4e0eelse \u5728\u540c\u4e00\u884c\uff0c else \u4e5f\u5fc5\u987b\u4e0e\u4e0a\u4e00\u4e2aif \u6216else if \u7684\u53f3\u8fb9\u7684\u5927\u62ec\u53f7\u5728\u4e00\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a\u4e0b\u9762\u5b9a\u4e49\u4e24\u4e2a\u53d8\u91cf\uff0c\u901a\u8fc7if\u8bed\u53e5\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"506\" height=\"318\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/20.png\" alt=\"\" class=\"wp-image-2860\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/20.png 506w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/20-300x189.png 300w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">if \u8fd8\u6709\u4e00\u79cd\u7279\u6b8a\u7684\u5199\u6cd5\uff0c\u53ef\u4ee5\u5728if \u8868\u8fbe\u5f0f\u4e4b\u524d\u6dfb\u52a0\u4e00\u4e2a\u6267\u884c\u8bed\u65ec\uff0c\u518d\u6839\u636e<strong>\u53d8\u91cf\u503c<\/strong>\u8fdb\u884c\u5224\u65ad\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if err := Connect(); err !=nil {\nfmt . Println(err)\nreturn\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Connect \u662f\u4e00\u4e2a\u5e26\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c err  :\uff1d Connect()\u662f\u4e00\u4e2a\u8bed\u53e5\uff0c\u6267\u884cConnect \u540e\uff0c\u5c06\u9519\u8bef\u4fdd\u5b58\u5230err\u53d8\u91cf\u4e2d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">err !=nil \u624d\u662fif \u7684\u5224\u65ad\u8868\u8fbe\u5f0f\uff0c\u5f53err \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6253\u5370\u9519\u8bef\u5e76\u8fd4\u56de\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u5199\u6cd5\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u4e0e\u5224\u65ad\u653e\u5728\u4e00\u884c\u8fdb\u884c\u5904\u7406\uff0c\u800c\u4e14\u8fd4\u56de\u503c\u7684\u4f5c\u7528\u8303\u56f4\u88ab\u9650\u5236\u5728<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if\u3001else \u8bed\u53e5\u7ec4\u5408\u4e2d<\/p>\n\n\n\n<p class=\"has-vivid-purple-color has-text-color wp-block-paragraph\">\u5728\u7f16\u7a0b\u4e2d\uff0c\u53d8\u91cf\u5728\u5176\u5b9e\u73b0\u4e86\u53d8\u91cf\u7684\u529f\u80fd\u540e\uff0c\u4f5c\u7528\u8303\u56f4\u8d8a\u5c0f\uff0c\u6240\u9020\u6210\u7684\u95ee\u9898\u53ef\u80fd\u6027\u8d8a\u5c0f\uff0c\u6bcf\u4e00\u4e2a\u53d8\u91cf\u4ee3\u8868\u4e00\u4e2a\u72b6\u6001\uff0c\u6709\u72b6\u6001\u7684\u5730\u65b9\uff0c\u72b6\u6001\u5c31\u4f1a\u88ab\u6539\uff0c\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u53ea\u4f1a\u5f71\u54cd\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\uff0c \u4f46\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u4f1a\u5f71\u54cd\u6240\u6709\u4ee3\u7801\u7684\u6267\u884c\u72b6\u6001\uff0c\u56e0\u6b64<strong>\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u8303\u56f4\u5bf9\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u6709\u5f88\u5927\u7684\u5e2e\u52a9<\/strong>\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u6784\u5efa\u5faa\u73af(for)\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u4e2d\u7684\u6240\u6709\u5faa\u73af\u7c7b\u578b\u5747\u53ef\u4ee5\u4f7f\u7528for \u5173\u952e\u5b57\u6765\u5b8c\u6210\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for \u521d\u59cb\u8bed\u53e5; \u6761\u4ef6\u8868\u8fbe\u5f0f; \u7ed3\u675f\u8bed\u53e5\uff5b\n\u5faa\u73af\u4f53\u4ee3\u7801\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u73af\u4f53\u4e0d\u505c\u5730\u8fdb\u884c\u5faa\u73af\uff0c\u76f4\u5230\u6761\u4ef6\u8868\u8fbe\u5f0f\u8fd4\u56defalse \u65f6\u81ea\u52a8\u9000\u51fa\u5faa\u73af\uff0c\u6267\u884cfor \u7684&#8221;}&#8221;\u4e4b\u540e\u7684\u8bed\u53e5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">for \u5faa\u73af\u53ef\u4ee5\u901a\u8fc7break \u3001goto \u3001return\u3001panic \u8bed\u53e5\u5f3a\u5236\u9000\u51fa\u5faa\u73af\u3002for \u7684\u521d\u59cb\u8bed\u53e5\u3001\u6761\u4ef6\u8868\u8fbe\u5f0f\u3001\u7ed3\u675f\u8bed\u53e5\u7684\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4e0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1<strong>\u3001for \u4e2d\u7684\u521d\u59cb\u8bed\u53e5&#8212;-\u5f00\u59cb\u5faa\u73af\u65f6\u6267\u884c\u7684\u8bed\u53e5<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521d\u59cb\u8bed\u53e5\u662f\u5728\u7b2c\u4e00\u6b21\u5faa\u73af\u524d\u6267\u884c\u7684\u8bed\u65ec\uff0c\u4e00\u822c\u4f7f\u7528\u521d\u59cb\u8bed\u53e5\u6267\u884c\u53d8\u91cf\u521d\u59cb\u5316\uff0c\u5982\u679c\u53d8\u91cf\u5728\u6b64\u5904\u88ab\u58f0\u660e\uff0c\u5176\u4f5c\u7528\u57df\u5c06\u88ab\u5c40\u9650\u5728\u8fd9\u4e2afor \u7684\u8303\u7574\u5185<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521d\u59cb\u8bed\u53e5\u53ef\u4ee5\u88ab\u5ffd\u7565\uff0c\u4f46\u662f\u521d\u59cb\u8bed\u53e5\u4e4b\u540e\u7684\u5206\u53f7\u5fc5\u987b\u8981\u5199\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>step : = 2\nfor ; step &gt; 0; step-- {\nfmt . Println(step)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6bb5\u4ee3\u7801\u5c06step \u653e\u5728for \u7684\u524d\u9762\u8fdb\u884c\u521d\u59cb\u5316\uff0c for \u4e2d\u6ca1\u6709\u521d\u59cb\u8bed\u53e5\uff0c \u6b64\u65f6step \u7684\u4f5c\u7528\u57df\u5c31\u6bd4\u5728\u521d\u59cb\u8bed\u53e5\u4e2d\u58f0\u660estep\u8981\u5927<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2\u3001for \u4e2d\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f<\/strong>&#8212;<strong>\u63a7\u5236\u662f\u5426\u5faa\u73af\u7684\u5f00\u5173<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u6bcf\u6b21\u5faa\u73af\u5f00\u59cb\u524d\u8ba1\u7b97\u7684\u8868\u8fbe\u5f0f\uff0c \u5982\u679c\u8868\u8fbe\u5f0f\u4e3atrue \uff0c\u5219\u5faa\u73af\u7ee7\u7eed\uff0c\u5426\u5219\u7ed3\u675f\u5faa\u73af\u3002\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u88ab\u5ffd\u7565\uff0c\u88ab\u5ffd\u7565\u6761\u4ef6\u7684\u8868\u8fbe\u5f0f\u9ed8\u8ba4\u5f62\u6210\u65e0\u9650\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u675f\u5faa\u73af\u65f6\u5e26\u53ef\u6267\u884c\u8bed\u53e5\u7684\u65e0\u9650\u5faa\u73af\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1a\u4e0b\u9762\u4ee3\u7801\u5ffd\u7565\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u4f46\u662f\u4fdd\u7559\u4e86\u7ed3\u675f\u8bed\u53e5\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"243\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/1-1.png\" alt=\"\" class=\"wp-image-2861\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/1-1.png 515w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/1-1-300x142.png 300w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/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>\u65e0\u987b\u8bbe\u7f6ei \u7684\u521d\u59cb\u503c\uff0c \u56e0\u6b64\u5ffd\u7565for \u7684\u521d\u59cb\u8bed\u53e5\u3002\u4e24\u4e2a\u5206\u53f7\u4e4b\u95f4\u662f\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c \u4e5f\u88ab\u5ffd\u7565\uff0c\u6b64\u65f6\u5faa\u73af\u4f1a\u4e00\u76f4\u6301\u7eed\u4e0b\u53bb\uff1a for \u7684\u7ed3\u675f\u8bed\u53e5\u4e3ai++ \uff0c\u6bcf\u6b21\u7ed3\u675f\u5faa\u73af\u524d\u90fd\u4f1a\u8c03\u7528<\/li>\n\n\n\n<li>\u5224\u65adi \u5927\u4e8e1 0 \u65f6\uff0c\u901a\u8fc7break \u8bed\u53e5\u8df3\u51fafor \u5faa\u73af<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u65e0\u9650\u5faa\u73af\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\u8fd8\u53ef\u4ee5\u6539\u5199\u4e3a\u66f4\u7f8e\u89c2\u7684\u5199\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"516\" height=\"316\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/2-1.png\" alt=\"\" class=\"wp-image-2862\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/2-1.png 516w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/2-1-300x184.png 300w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><\/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>\u5ffd\u7565for \u7684\u6240\u6709\u8bed\u53e5\uff0c\u6b64\u65f6for \u6267\u884c\u65e0\u9650\u5faa\u73af<\/li>\n\n\n\n<li>\u5c06i \uff0b\uff0b\u4ecefor \u7684\u7ed3\u675f\u8bed\u53e5\u653e\u7f6e\u5230\u51fd\u6570\u4f53\u7684\u672b\u5c3e\u662f\u7b49\u6548\u7684\uff0c\u8fd9\u6837\u7f16\u5199\u7684\u4ee3\u7801\u66f4\u5177\u6709\u53ef\u8bfb\u6027<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u65e0\u9650\u5faa\u73af\u5728\u6536\u53d1\u5904\u7406\u4e2d\u8f83\u4e3a\u5e38\u89c1\uff0c \u4f46\u9700\u8981\u65e0\u9650\u5faa\u73af\u6709\u53ef\u63a7\u7684\u9000\u51fa\u65b9\u5f0f\u6765\u7ed3\u675f\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53ea\u6709\u4e00\u4e2a\u5faa\u73af\u6761\u4ef6\u7684\u5faa\u73af\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\u8fdb\u4e00\u6b65\u7b80\u5316\u4ee3\u7801\uff0c\u5c06if \u5224\u65ad\u6574\u5408\u5230for \u4e2d\uff0c\u53d8\u4e3a\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"239\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/3-2.png\" alt=\"\" class=\"wp-image-2863\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/3-2.png 515w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/3-2-300x139.png 300w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/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=\"549\" height=\"239\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-4.png\" alt=\"\" class=\"wp-image-6591\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-4.png 549w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/11-4-300x131.png 300w\" sizes=\"auto, (max-width: 549px) 100vw, 549px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u5176\u5b9e\u7c7b\u4f3c\u4e8e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684while \uff0c\u5728while \u540e\u6dfb\u52a0\u4e00\u4e2a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u6ee1\u8db3\u6761\u4ef6\u8868\u8fbe\u5f0f\u65f6\u6301\u7eed\u5faa\u73af\uff0c\u5426\u5219\u7ed3\u675f\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u952e\u503c\u5faa\u73af(for range)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u53ef\u4ee5\u4f7f\u7528for range \u904d\u5386\u6570\u7ec4\u3001\u5207\u7247\u3001\u5b57\u7b26\u4e32\u3001map \u53ca\u901a\u9053\uff08 channel \uff09\u901a\u8fc7for range \u904d\u5386\u7684\u8fd4\u56de\u503c\u6709\u4e00\u5b9a\u7684\u89c4\u5f8b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6570\u7ec4\u3001\u5207\u7247\u3001\u5b57\u7b26\u4e32\u8fd4\u56de\u7d22\u5f15\u548c\u503c<\/li>\n\n\n\n<li>map \u8fd4\u56de\u952e\u548c\u503c\u3002<\/li>\n\n\n\n<li>\u901a\u9053\uff08 channel \uff09\u53ea\u8fd4\u56de\u901a\u9053\u5185\u7684\u503c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u904d\u5386\u6570\u7ec4\u3001\u5207\u7247&#8212;-\u83b7\u5f97\u7d22\u5f15\u548c\u5143\u7d20<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"339\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/4.png\" alt=\"\" class=\"wp-image-2864\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/4.png 552w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/4-300x184.png 300w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u904d\u5386\u5b57\u7b26\u4e32&#8212;&#8211;\u83b7\u5f97\u5b57\u7b26<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"526\" height=\"221\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/5.png\" alt=\"\" class=\"wp-image-2866\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/5.png 526w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/5-300x126.png 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u904d\u5386map&#8212;-\u83b7\u53d6map\u7684\u952e\u548c\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8emap \u7c7b\u578b\u6765\u8bf4\uff0c for range \u904d\u5386\u65f6\uff0c key \u548cvalue \u5206\u522b\u4ee3\u8868map \u7684\u7d22\u5f15\u952ekey \u548c\u7d22\u5f15\u5bf9\u5e94\u7684\u503c\uff0c \u4e00\u822c\u88ab\u79f0\u4e3amap \u7684\u952e\u503c\u5bf9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4f8b\u5b50\u8868\u793a\u904d\u5386map\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"322\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/6.png\" alt=\"\" class=\"wp-image-2875\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/6.png 545w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/6-300x177.png 300w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u4e0a\u65b9\u6bcf\u4e2a\u5143\u7d20\u540e\u9762\u90fd\u6709\u9017\u53f7\uff0c\u5bf9\u4e8emap\u7684\u904d\u5386\uff0c\u503c\u662f\u65e0\u5e8f\u7684\uff0c\u5982\u679c\u8981\u6307\u5b9a\u987a\u5e8f\uff0c\u9700\u8981\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u904d\u5386\u901a\u9053(channel)&#8212;\u63a5\u6536\u901a\u9053\u6570\u636e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">for range \u53ef\u4ee5\u904d\u5386\u901a\u9053\uff08 channel \uff09 \uff0c\u4f46\u662f\u901a\u9053\u5728\u904d\u5386\u65f6\uff0c\u53ea\u8f93\u51fa\u4e00\u4e2a\u503c\uff0c\u5373\u7ba1\u9053\u5185\u7684\u7c7b\u578b\u5bf9\u5e94\u7684\u6570\u636e\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"359\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/7.png\" alt=\"\" class=\"wp-image-2876\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/7.png 548w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/7-300x197.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">5\u3001\u5728\u904d\u5386\u4e2d\u9009\u62e9\u5e0c\u671b\u83b7\u5f97\u7684\u53d8\u91cf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528for range \u5faa\u73af\u904d\u5386\u67d0\u4e2a\u5bf9\u8c61\u65f6\uff0c \u4e00\u822c\u4e0d\u4f1a\u540c\u65f6\u9700\u8981key \u6216\u8005value \uff0c \u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u91c7\u7528\u4e00\u4e9b\u6280\u5de7\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u7b80\u5355\u3002\u4e0b\u9762\u5c06\u524d\u9762\u7684\u4f8b\u5b50\u4fee\u6539\u4e00\u4e0b\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"555\" height=\"326\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/8.png\" alt=\"\" class=\"wp-image-2877\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/8.png 555w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/8-300x176.png 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\u7684\u4f8b\u5b50\u662f\u5c06\u952e\u8bbe\u7f6e\u4e3a\u533f\u540d\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u5c06\u503c\u7528\u533f\u540d\u53d8\u91cf\u66ff\u4ee3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u603b\u7ed3\u4e00\u4e0bfor \u7684\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go \u8bed\u8a00\u7684for \u5305\u542b\u521d\u59cb\u5316\u8bed\u53e5\u3001\u6761\u4ef6\u8868\u8fbe\u5f0f\u3001\u7ed3\u675f\u8bed\u65ec\uff0c \u8fd93 \u4e2a\u90e8\u5206\u5747\u53ef\u7f3a\u7701\u3002<\/li>\n\n\n\n<li>for range \u652f\u6301\u5bf9\u6570\u7ec4\u3001\u5207\u7247\u3001\u5b57\u7b26\u4e32\u3001map \u3001\u901a\u9053\u8fdb\u884c\u904d\u5386\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>\u5728\u9700\u8981\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u53d8\u91cf\u5bf9for range \u7684\u53d8\u91cf\u8fdb\u884c\u9009\u53d6\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">6\u3001\u5728golang1.22\u5f00\u59cb\uff0cfor&#8230;range\u652f\u6301\u904d\u5386\u6574\u6570\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"694\" height=\"298\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/7-4.jpg\" alt=\"\" class=\"wp-image-16346\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/7-4.jpg 694w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/7-4-300x129.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/7-4-600x258.jpg 600w\" sizes=\"auto, (max-width: 694px) 100vw, 694px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"174\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/6-3.jpg\" alt=\"\" class=\"wp-image-16345\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/6-3.jpg 695w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/6-3-300x75.jpg 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2024\/03\/6-3-600x150.jpg 600w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1arange 6\u7684\u503c\u662f\u4ece0\u5f00\u59cb\uff0c\u4f46\u662f\u4e0d\u5305\u62ec6\uff0cgo1.22\u4ee5\u524d\u7248\u672c\u4f1a\u62a5\u9519cannot range over a (variable of type int)<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u5206\u652f\u9009\u62e9(switch)&#8212;\u62e5\u6709\u591a\u4e2a\u6761\u4ef6\u5206\u652f\u7684\u5224\u65ad<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go \u8bed\u8a00\u4e2d\u7684switch \uff0c\u4e0d\u4ec5\u53ef\u4ee5\u57fa\u4e8e\u5e38\u91cf\u8fdb\u884c\u5224\u65ad\uff0c\u8fd8\u53ef\u4ee5\u57fa\u4e8e\u8868\u8fbe\u5f0f\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u7684switch \u4e2d\u7684\u6bcf\u4e00\u4e2acase \u4e0ecase \u95f4\u662f\u72ec\u7acb\u7684\u4ee3\u7801\u5757\uff0c\u4e0d\u9700\u8981\u901a\u8fc7break \u8bed\u53e5\u8df3\u51fa\u5f53\u524dcase \u4ee3\u7801\u5757\u4ee5\u907f\u514d\u6267\u884c\u5230\u4e0b\u4e00\u884c\uff0c\u5982\u4e0b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"306\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/13-5.png\" alt=\"\" class=\"wp-image-6594\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/13-5.png 553w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/13-5-300x166.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"560\" height=\"92\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/15-4.png\" alt=\"\" class=\"wp-image-6598\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/15-4.png 560w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/15-4-300x49.png 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e00\u4e2acase \u5747\u662f\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u4e14\u4f7f\u7528\u4e86default \u5206\u652f\uff0c Go \u8bed\u8a00\u89c4\u5b9a\u6bcf\u4e2aswitch \u53ea\u80fd\u6709\u4e00\u4e2adefault \u5206\u652f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e00\u5206\u652f\u591a\u503c\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u51fa\u73b0\u591a\u4e2acase \u8981\u653e\u5728\u4e00\u8d77\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u50cf\u4e0b\u9762\u4ee3\u7801\u8fd9\u6837\u5199\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"555\" height=\"318\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-5.png\" alt=\"\" class=\"wp-image-6596\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-5.png 555w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/14-5-300x172.png 300w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6700\u7ec8\u7684\u6253\u5370\u7ed3\u679c\u4e3a\u5339\u914d\uff0c\u8bf4\u660e\u4e0a\u8ff0\u7684case\u53ea\u8981\u5339\u914d\u4e00\u4e2a\u5c31\u7b97\u6210\u529f\uff0c\u76f8\u5f53\u4e8e&#8221;\u6216&#8221; \u6761\u4ef6\uff0c\u6ce8\u610f\uff1a\u4e0d\u540c\u7684case \u8868\u8fbe\u5f0f\u4f7f\u7528\u9017\u53f7\u5206\u9694<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u5206\u652f\u8868\u8fbe\u5f0f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">case \u540e\u4e0d\u4ec5\u4ec5\u53ea\u662f\u5e38\u91cf\uff0c\u8fd8\u53ef\u4ee5\u548cif \u4e00\u6837\u6dfb\u52a0\u8868\u8fbe\u5f0f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"254\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/11.png\" alt=\"\" class=\"wp-image-2881\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/11.png 552w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/11-300x138.png 300w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684&amp;&amp;\u8868\u793a\u4e0e\u6761\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u6216(||)\u6761\u4ef6<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff0c\u8fd9\u79cd\u60c5\u51b5\u7684switch \u540e\u9762\u4e0d\u518d\u8ddf\u5224\u65ad\u53d8\u91cf\uff0c \u8fde\u5224\u65ad\u7684\u76ee\u6807\u90fd\u6ca1\u6709\u4e86<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u8de8\u8d8acase \u7684fallthrough<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go \u8bed\u8a00\u4e2dcase \u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u4ee3\u7801\u5757\uff0c\u6267\u884c\u5b8c\u6bd5\u540e\u4e0d\u4f1a\u50cfC \u8bed\u8a00\u90a3\u6837\u7d27\u63a5\u7740\u4e0b\u4e00\u4e2acase \u6267\u884c\u3002\u4f46\u662f\u4e3a\u4e86\u517c\u5bb9\u4e00\u4e9b\u79fb\u690d\u4ee3\u7801\uff0c\u4f9d\u7136\u52a0\u5165\u4e86fallthrough \u5173\u952e\u5b57\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"560\" height=\"340\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/16-3.png\" alt=\"\" class=\"wp-image-6600\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/16-3.png 560w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/16-3-300x182.png 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"109\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/17-3.png\" alt=\"\" class=\"wp-image-6603\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/17-3.png 557w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/17-3-300x59.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">switch \u8bed\u53e5\u53ef\u7528\u6765\u66ff\u4ee3\u5197\u957f\u7684if else \u5e03\u5c14\u6bd4\u8f83\uff0c\u4ece\u7f16\u8bd1\u5c42\u9762\u4e0a\u8bf4\uff0cswitch\u4ee3\u7801\u6548\u7387\u66f4\u9ad8\u3002\u9664\u5c11\u91cf\u7684 if else \u5916\uff0c\u5176\u4ed6\u7684\u90fd\u53ef\u66ff\u6362\u4e3a switch \u8bed\u53e5,\u4e0d\u4ec5\u53ef\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u63d0\u9ad8\u5176\u6027\u80fd\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u8df3\u8f6c\u5230\u6307\u5b9a\u4ee3\u7801\u6807\u7b7e(goto)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">goto \u8bed\u53e5\u901a\u8fc7\u6807\u7b7e\u8fdb\u884c\u4ee3\u7801\u95f4\u7684\u65e0\u6761\u4ef6\u8df3\u8f6c\u3002goto \u8bed\u53e5\u53ef\u4ee5\u5728\u5feb\u901f\u8df3\u51fa\u5faa\u73af\u3001\u907f\u514d\u91cd\u590d\u9000\u51fa\u4e0a\u6709\u4e00\u5b9a\u7684\u5e2e\u52a9\u3002Go \u8bed\u8a00\u4e2d\u4f7f\u7528goto \u8bed\u53e5\u80fd\u7b80\u5316\u4e00\u4e9b\u4ee3\u7801\u7684\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4f7f\u7528goto \u9000\u51fa\u591a\u5c42\u5faa\u73af<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\uff0c\u9700\u8981\u8fde\u7eed\u9000\u51fa\u4e24\u5c42\u5faa\u73af\uff0c\u4f7f\u7528\u4f20\u7edf\u7684\u7f16\u7801\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"396\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/13.png\" alt=\"\" class=\"wp-image-2885\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/13.png 551w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/13-300x216.png 300w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u4f7f\u7528goto \u96c6\u4e2d\u5904\u7406\u9519\u8bef\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528Go \u8bed\u8a00\u7684goto \u8bed\u53e5\u8fdb\u884c\u4f18\u5316\uff0c\u6807\u7b7e\u53ef\u81ea\u5b9a\u4e49\u8bbe\u7f6e\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"564\" height=\"374\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/18-2.png\" alt=\"\" class=\"wp-image-6606\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/18-2.png 564w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/18-2-300x199.png 300w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u56fe\u4e2d\u5f53\u6ee1\u8db3\u6761\u4ef6y==2\u7684\u65f6\u5019\uff0c\u5c06\u4f1a\u8df3\u51fa\u5faa\u73af\uff0c\u8df3\u5230\u6807\u7b7egongguan\u4f4d\u7f6e\uff0c\u6267\u884c\u6807\u7b7e\u540e\u9762\u7684\u5185\u5bb9\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"568\" height=\"148\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/19-2.png\" alt=\"\" class=\"wp-image-6608\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/19-2.png 568w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/19-2-300x78.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e0a\u9762\u7684return<\/strong>\uff0c\u5982\u679c\u4e0d\u5199return\uff0c\u90a3\u4e48\u5f53\u6761\u4ef6\u6ee1\u8db3\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u6267\u884c\u6807\u7b7egongguan\u6700\u4e0b\u9762\u7684fmt.Println(&#8220;\u5f53y=2\u7684\u65f6\u5019\uff0c\u8df3\u51fa\u6240\u6709\u5faa\u73af&#8221;)\uff0c\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u6267\u884c fmt.Println(&#8220;\u5f53y=2\u7684\u65f6\u5019\uff0c\u8df3\u51fa\u6240\u6709\u5faa\u73af&#8221;) \uff0c\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u8981\u60f3\u5b9e\u73b0\u6ee1\u8db3\u6761\u4ef6\u624d\u6267\u884c\u6807\u7b7e\u4e0b\u65b9\u7684\u5185\u5bb9\uff0c\u4e0d\u6ee1\u8db3\u4e0d\u6267\u884c\uff0c\u90a3\u4e48\u5c31\u8981\u52a0\u4e0areturn\uff0c\u5982\u4e0b\u56fe\uff0c\u5c06return\u6ce8\u91ca\uff0c\u5f53\u6761\u4ef6\u4e0d\u6ee1\u8db3\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u53d1\u73b0\u6807\u7b7e\u4e0b\u7684\u5185\u5bb9\u8fd8\u662f\u4f1a\u6267\u884c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"566\" height=\"345\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/20-1.png\" alt=\"\" class=\"wp-image-6609\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/20-1.png 566w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/20-1-300x183.png 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"561\" height=\"205\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/21-1.png\" alt=\"\" class=\"wp-image-6611\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/21-1.png 561w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/21-1-300x110.png 300w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528goto \u8bed\u53e5\u540e\uff0c\u65e0\u987b\u989d\u5916\u7684\u53d8\u91cf\u5c31\u53ef\u4ee5\u5feb\u901f\u9000\u51fa\u6240\u6709\u7684\u5faa\u73af<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u7edf\u4e00\u9519\u8bef\u5904\u7406\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u5904\u9519\u8bef\u5904\u7406\u5b58\u5728\u4ee3\u7801\u91cd\u590d\u65f6\u662f\u975e\u5e38\u68d8\u624b\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"385\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/15.png\" alt=\"\" class=\"wp-image-2887\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/15.png 551w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/15-300x210.png 300w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u4ee3\u7801\u662f\u91cd\u590d\uff0c\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7goto\u6765\u5904\u7406\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"406\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/16.png\" alt=\"\" class=\"wp-image-2888\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/16.png 552w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/01\/16-300x221.png 300w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong>\u8df3\u51fa\u6307\u5b9a\u5faa\u73af(break)&#8212;\u53ef\u4ee5\u8df3\u51fa\u591a\u5c42\u5faa\u73af<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">break \u8bed\u53e5\u53ef\u4ee5\u7ed3\u675ffor \u3001switch \u548cselect \u7684\u4ee3\u7801\u5757\u3002break \u8bed\u53e5\u8fd8\u53ef\u4ee5\u5728\u8bed\u53e5\u540e\u9762\u6dfb\u52a0\u6807\u7b7e\uff0c\u8868\u793a\u9000\u51fa\u67d0\u4e2a\u6807\u7b7e\u5bf9\u5e94\u7684\u4ee3\u7801\u5757\uff0c\u6807\u7b7e\u8981\u6c42\u5fc5\u987b\u5b9a\u4e49\u5728\u5bf9\u5e94\u7684for \u3001switch \u548cselect\u7684\u4ee3\u7801\u5757\u4e0a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"567\" height=\"394\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/22-1.png\" alt=\"\" class=\"wp-image-6614\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/22-1.png 567w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/22-1-300x208.png 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"562\" height=\"158\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/23-1.png\" alt=\"\" class=\"wp-image-6617\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/23-1.png 562w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/23-1-300x84.png 300w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong>\u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af<\/strong>(continue)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">continue \u8bed\u53e5\u53ef\u4ee5\u7ed3\u675f\u5f53\u524d\u5faa\u73af\uff0c \u5f00\u59cb\u4e0b\u4e00\u6b21\u7684\u5faa\u73af\u8fed\u4ee3\u8fc7\u7a0b\uff0c \u4ec5\u9650\u5728for \u5faa\u73af\u5185\u4f7f\u7528\u3002\u5728continue \u8bed\u53e5\u540e\u6dfb\u52a0\u6807\u7b7e\u65f6\uff0c \u8868\u793a\u5f00\u59cb\u6807\u7b7e\u5bf9\u5e94\u7684\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"566\" height=\"353\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/24-1.png\" alt=\"\" class=\"wp-image-6619\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/24-1.png 566w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/24-1-300x187.png 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"333\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/25-1.png\" alt=\"\" class=\"wp-image-6621\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/25-1.png 570w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/01\/25-1-300x175.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong>\u4f7f\u7528\u5ef6\u8fdf\u6267\u884c\u8bed\u53e5(defer)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">defer\u8bed\u53e5\u80fd\u591f\u5728\u51fd\u6570\u8fd4\u56de\u524d\u6267\u884c\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u51fd\u6570\u5728\u9047\u5230return\u8bed\u53e5\u6216\u8005\u5230\u8fbe\u51fd\u6570\u672b\u5c3e\u65f6\u8fd4\u56de\uff0cdefer\u8bed\u53e5\u901a\u5e38\u7528\u4e8e\u6267\u884c\u6e05\u7406\u64cd\u4f5c\u6216\u8005\u786e\u4fdd\u64cd\u4f5c(\u5982\u7f51\u8def\u8c03\u7528)\uff0c\u5b8c\u6210\u540e\u6267\u884c\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u5b9a\u4e49\u4e86\u4e09\u6761defer\u548c\u4e00\u6761\u6b63\u5e38\u7684\u51fd\u6570\u8bed\u53e5\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"219\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/6.png\" alt=\"\" class=\"wp-image-3596\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/6.png 620w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/6-300x106.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/6-600x212.png 600w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/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=\"553\" height=\"152\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/8.png\" alt=\"\" class=\"wp-image-3597\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/8.png 553w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2021\/04\/8-300x82.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u51fd\u6570\u5185\u90e8\u6b63\u5e38\u6267\u884c\u5b8c\u6210\u540e\uff0c\u5f00\u59cb\u6267\u884cdefer\u8bed\u53e5\uff0c\u5e76\u4e14\u6309\u7167defer\u5b9a\u4e49\u7684\u987a\u5e8f\u76f8\u53cd\u7684\u987a\u5e8f\u6267\u884c\u8bed\u53e5\uff0c\u56e0\u6b64\u662f\u7b2c\u4e09\u4e2adefer\u8bed\u53e5\u5148\u6267\u884c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go \u8bed\u8a00\u7684\u5e38\u7528\u6d41\u7a0b\u63a7\u5236\u6709if \u548cfor \uff0c\u800cswitch \u548cgoto \u4e3b\u8981\u662f\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\u3001\u964d\u4f4e\u91cd\u590d\u4ee3\u7801\u800c\u751f\u7684 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2844,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-2846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"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\/2846","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=2846"}],"version-history":[{"count":37,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2846\/revisions"}],"predecessor-version":[{"id":16347,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2846\/revisions\/16347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/2844"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}