{"id":1536,"date":"2020-09-22T06:20:38","date_gmt":"2020-09-21T22:20:38","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1536"},"modified":"2022-02-16T21:58:10","modified_gmt":"2022-02-16T13:58:10","slug":"go-forloop-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/09\/22\/go-forloop-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u8ff4\u5708\u7684\u8a9e\u6cd5"},"content":{"rendered":"<h1>go \u8ff4\u5708\u7684\u8a9e\u6cd5<\/h1>\n<p>\u5728\u4ecb\u7d39\u5b8c go \u7684\u908f\u8f2f\u5224\u65b7\u7684\u4e4b\u5f8c\u63a5\u4e0b\u4f86\u8981\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b go \u8ff4\u5708\u7684\u8a9e\u6cd5\uff0c\u9019\u90e8\u5206\u5167\u5bb9\u8207\u5927\u90e8\u5206\u8a9e\u8a00\u76f8\u540c\uff0c\u672c\u4f86\u7b46\u8005\u60f3\u9019\u6a23\u544a\u8a34\u5927\u5bb6\uff0c\u4f46\u662f\u5176\u5be6 go \u53ea\u6709\u4e00\u500b<code>for<\/code>\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u7b46\u8005\u6703\u8aaa\u660e\u5982\u4f55\u7528\u4e00\u500b<code>for<\/code>\u4f86\u5b8c\u6210\u5176\u4ed6\u8a9e\u8a00\u7684<code>for<\/code>\u3001<code>foreach<\/code>\u3001<code>while<\/code>\u9019\u7a2e\u8ff4\u5708\u5224\u65b7\u5f0f\u3002<\/p>\n<p><!--more--><\/p>\n<h2>go \u7684 for\u8ff4\u5708<\/h2>\n<p>\u56e0\u70ba go \u7684\u8ff4\u5708\u8a9e\u6cd5\u53ea\u6709<code>for<\/code>\u4e00\u7a2e\u6240\u4ee5\u63a5\u4e0b\u4f86\u6703\u4ee5\u5176\u4ed6\u8a9e\u8a00\u7684<code>for<\/code>\u3001<code>foreach<\/code>\u3001<code>while<\/code>\u4f86\u89e3\u91cb\u5982\u4f55\u5728 go \u88e1\u9762\u5b8c\u6210\u3002<\/p>\n<h3>for<\/h3>\n<p>\u6240\u4ee5\u5148\u91dd\u5c0d<code>for<\/code>\u8ff4\u5708\u7684\u5beb\u6cd5\u4f86\u4ecb\u7d39\uff0c\u8acb\u5927\u5bb6\u770b\u4e00\u4e0b\u4ee5\u4e0b\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    sum := 0\n    for i := 0; i &lt; 10; i++ {\n        sum += i\n    }\n    fmt.Println(sum)\n}<\/code><\/pre>\n<p>\u770b\u4e0a\u8ff0\u4f8b\u5b50\u5176\u5be6\u8207\u5176\u4ed6\u8a9e\u8a00\u5dee\u7570\u4e0d\u5927\uff0c\u7c21\u55ae\u8b1b\u5c31\u662f<code>for ; ; {}<\/code>\u7684\u7d50\u69cb<code>for<\/code>\u8207\u7b2c\u4e00\u500b<code>;<\/code>\u662f<code>for<\/code>\u8ff4\u5708\u7684\u521d\u59cb\u8a2d\u5b9a\uff0c\u5169\u500b<code>;<\/code>\u4e4b\u9593\u662f<code>for<\/code>\u8ff4\u5708\u7684\u5224\u65b7\u5f0f\uff0c<code>;<\/code>\u8207<code>{<\/code>\u5247\u662f\u5224\u65b7\u662f\u4e0d\u6210\u7acb\u6642\u8981\u505a\u7684\u5de5\u4f5c\u800c<code class=\"kb-btn\"><\/code>\u4e4b\u9593\u5247\u662f\u5728\u70ba\u6eff\u8db3\u6642\u57f7\u884c\u7684\u7a0b\u5f0f\u3002\u5728\u8aaa\u660e\u5b8c<code>for<\/code>\u8ff4\u5708\u5f8c\u8981\u4ecb\u7d39<code>foreach<\/code>\u7684\u5beb\u6cd5\u3002<\/p>\n<h2>foreach<\/h2>\n<p>\u5728 go \u6c92\u6709<code>foreach<\/code>\u7684\u8a9e\u6cd5\uff0c\u4e0d\u904e\u9084\u662f\u53ef\u4ee5\u505a\u5230<code>foreach<\/code>\u7684\u5de5\u4f5c\uff0c\u95dc\u65bc\u5982\u4f55\u8655\u7406<code>foreach<\/code>\u8acb\u770b\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n\n    numbers := []int{1,2,3,4,5,6,7,8,9}\n\n    for _, number:= range numbers {\n        fmt.Println(&quot;The number is:&quot;, number)\n    }\n}<\/code><\/pre>\n<p>\u8b80\u8005\u53ef\u4ee5\u770b\u5230\u9019\u4f8b\u5b50\u8abf\u7528<code>range<\/code>\u9019\u500b\u8a9e\u6cd5\u8ddf<code>_<\/code>\u9019\u500b\u8a9e\u6cd5\uff0c\u9996\u5148\u5148\u89e3\u91cb<code>_<\/code>\u9019\u500b\u8a9e\u6cd5\uff0c\u7531\u65bc\u5728<code>foreach<\/code>\u4e4b\u4e2d\u4e0d\u9700\u8981\u521d\u59cb\u5316\u6240\u4ee5\u7528\u4e00\u500b<code>_<\/code>\u4f86\u63a5\u4ed6\uff0c\u800c\u9019\u500b<code>_<\/code>\u5c08\u9580\u62ff\u4f86\u653e\u4e0d\u9700\u8981\u7684\u6771\u897f\u3002\u63a5\u4e0b\u4f86<code>range<\/code>\u5c31\u662f\u7528\u5728\u8fed\u4ee3\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u6b64\u5c31\u53ef\u4ee5\u5b8c\u6210<code>foreach<\/code>\u7684\u8a9e\u6cd5\uff0c\u6240\u4ee5\u5982\u679c\u4eca\u5929\u60f3\u8981\u4f9d\u5e8f\u8dd1<code>map<\/code>\u88e1\u9762\u7684\u5143\u7d20\u4e5f\u53ef\u4ee5\u7528\u9019\u7a2e\u65b9\u5f0f\uff0c\u5982\u4e0b\u5217\u6240\u793a<\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n\n    numbers := map[string]int{&quot;One&quot;:1,&quot;Two&quot;:2,&quot;Three&quot;:3}\n\n    for k, number:= range numbers {\n        fmt.Println(&quot;The &quot;, k, &quot; is:&quot;, number)\n    }\n}<\/code><\/pre>\n<p>\u539f\u4f86\u5728\u4e4b\u524d<code>slice<\/code>\u7684\u4f8b\u5b50\u4e2d\u6211\u5011\u5ffd\u7565\u7684\u5143\u7d20\u662f<code>index<\/code>\uff0c\u4f46\u662f\u5728<code>map<\/code>\u4e2d\u4e5f\u8a31\u6703\u4f7f\u7528\u5230\uff0c\u6240\u4ee5\u5728\u9019\u88e1\u7528<code>k<\/code>\u4f86\u63a5\u4ed6\uff0c\u5728<code>map<\/code>\u4e2d<code>k<\/code>\u6240\u53d6\u5230\u7684\u5c31\u662f<code>key<\/code>\uff0c\u5982\u679c\u5728<code>slice<\/code>\u9700\u8981<code>index<\/code>\u7684\u8a71\u4e5f\u53ef\u4ee5\u7528\u8b8a\u6578\u4f86\u63a5\u4ed6\uff0c\u9019\u90e8\u5206\u5c31\u4e0d\u8d05\u8ff0\u4e86\uff0c\u63a5\u4e0b\u4f86\u5c31\u8aaa\u660e\u4e00\u4e0b\u5982\u4f55\u8655\u7406<code>while<\/code>\u3002<\/p>\n<h3>while<\/h3>\n<p>\u5728 go \u8a9e\u8a00\u4e2d\u53ea\u6709<code>for<\/code>\u8ff4\u5708\u6c92\u6709<code>while<\/code>\u8ff4\u5708\u90a3\u662f\u56e0\u70ba<code>for<\/code>\u8ff4\u5708\u5c31\u80fd\u8655\u7406<code>while<\/code>\u8ff4\u5708\u7684\u5de5\u4f5c\u4e86\uff0c\u8acb\u770b\u4ee5\u4e0b\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n    sum := 1\n    for sum &lt; 1000 {\n        sum += 1\n    }\n    fmt.Println(sum)\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u662f\u5be6\u4f5c\u7576sum\u52a0\u7e3d\u52301000\u5c31\u505c\u6b62\u7684<code>while<\/code>\u8ff4\u5708\uff0c\u6240\u4ee5\u5beb\u6cd5\u8207\u5176\u5b83\u8a9e\u8a00\u5dee\u4e0d\u591a\uff0c\u53ea\u4e0d\u904e\u662f\u7528<code>for<\/code>\u95dc\u9375\u5b57\uff0c\u90a3\u8b80\u8005\u6703\u597d\u5947<code>while(true)<\/code>\u8981\u600e\u9ebc\u5be6\u4f5c\u5462\uff0c\u8acb\u770b\u4ee5\u4e0b\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">package main\n\nfunc main() {\n    for {\n    }\n}<\/code><\/pre>\n<p>\u7c21\u55ae\u8aaa\u660e\u5c31\u662f<code>for<\/code>\u4e4b\u5f8c\u4e0d\u5e36\u4efb\u4f55\u5224\u65b7\u5f0f\u5373\u53ef\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u9019\u7bc7\u6587\u7ae0\u8aaa\u660e go \u7684\u8ff4\u5708\u5224\u65b7\u5f0f\u8981\u5982\u4f55\u4f7f\u7528\uff0c\u4e26\u4e14\u9ede\u51fa go \u8a9e\u8a00\u7684\u8ff4\u5708\u5224\u65b7\u5f0f\u8207\u5176\u5b83\u8a9e\u8a00\u4e0d\u540c\u7684\u5730\u65b9\u3002\u5728\u8aaa\u660e\u5b8c\u8ff4\u5708\u5224\u65b7\u5f0f\u4e4b\u5f8c\u8981\u9032\u5165\u5982\u4f55\u5728 go \u88e1\u9762\u5beb\u65b9\u6cd5\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go \u8ff4\u5708\u7684\u8a9e\u6cd5 \u5728\u4ecb\u7d39\u5b8c go \u7684\u908f\u8f2f\u5224\u65b7\u7684\u4e4b\u5f8c\u63a5\u4e0b\u4f86\u8981\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b go \u8ff4\u5708\u7684\u8a9e\u6cd5\uff0c\u9019\u90e8\u5206\u5167\u5bb9\u8207\u5927\u90e8\u5206 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/09\/22\/go-forloop-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u8ff4\u5708\u7684\u8a9e\u6cd5\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81],"class_list":["post-1536","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1536"}],"version-history":[{"count":3,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1536\/revisions"}],"predecessor-version":[{"id":2684,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1536\/revisions\/2684"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}