{"id":2524,"date":"2015-11-08T23:18:52","date_gmt":"2015-11-08T15:18:52","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2524"},"modified":"2015-11-08T23:18:52","modified_gmt":"2015-11-08T15:18:52","slug":"golang%e4%b8%ad%e7%9a%84for-range%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2524.html","title":{"rendered":"Golang\u4e2d\u7684for&#8230;range\u8bed\u6cd5"},"content":{"rendered":"<p>=Start=<\/p>\n<h5>\u7f18\u7531\uff1a<\/h5>\n<p>Go\u8bed\u8a00\u4e2d\u7684for&#8230;range\u8bed\u6cd5\u5bf9\u4e8e\u521a\u63a5\u89e6\u7684\u4eba\u6765\u8bf4\u7a0d\u5fae\u6709\u90a3\u4e48\u70b9\u4e0d\u9002\u5e94\uff0c\u5bb9\u6613\u8e29\u5751\uff0c\u6240\u4ee5\u5728\u6b64\u8bb0\u5f55\u4e00\u4e0b\uff0c\u65b9\u4fbf\u67e5\u9605\u3002<\/p>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u5982\u679c\u5728range\u8868\u8fbe\u5f0f\u7684\u5de6\u8fb9\u53ea\u6709\u4e00\u4e2a\u53d8\u91cf\uff0c\u90a3\u5bf9\u5e94\u7684\u5c31\u662f\u4e0b\u8868\u4e2d\u7684\u300c1st value\u300d\u5217\uff08If only one value is used on the left of a range expression, it is the 1st value in this table.\uff09\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">Range expression<\/th>\n<th align=\"left\">1st value<\/th>\n<th align=\"left\">2nd value (optional)<\/th>\n<th align=\"left\">notes<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\">array or slice a <code>[n]E<\/code>,<code>*[n]E<\/code>, or <code>[]E<\/code><\/td>\n<td align=\"left\">index <code>i int<\/code><\/td>\n<td align=\"left\"><code>a[i]<\/code> E<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\">string s string type<\/td>\n<td align=\"left\">index <code>i int<\/code><\/td>\n<td align=\"left\">rune <code>int<\/code><\/td>\n<td align=\"left\">range iterates over Unicode code points, not bytes<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">map m <code>map[K]V<\/code><\/td>\n<td align=\"left\">key <code>k K<\/code><\/td>\n<td align=\"left\">value<code>m[k]<\/code> V<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\">channel c chan E<\/td>\n<td align=\"left\">element<code>e E<\/code><\/td>\n<td align=\"left\"><em>none<\/em><\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6837\u4f8b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">for k, v := range myMap {\n    log.Printf(\"key=%v, value=%v\", k, v)\n}\n\nfor v := range myChannel {\n    log.Printf(\"value=%v\", v)\n}\n\nfor i, v := range myArray {\n    log.Printf(\"array value at [%d]=%v\", i, v)\n}\n\n\/\/ `range` on strings iterates over Unicode code points.\n\/\/ The first value is the starting byte index of the `rune` and the second the `rune` itself.\nfor i, c := range \"go\" {\n    fmt.Println(i, c)\n}\n\n\/* ==== *\/\n\nvar pow = []int{1, 2, 4, 8, 16, 32, 64, 128}\nfor key, value := range pow {\n\tfmt.Printf(\"%d\\t%d\\n\", key, value)\n}\nfor key := range pow {\n\tfmt.Printf(\"%d\\t\", key)\n}<\/pre>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/gobyexample.com\/range\" target=\"_blank\">https:\/\/gobyexample.com\/range<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/golang\/go\/wiki\/Range\" target=\"_blank\">https:\/\/github.com\/golang\/go\/wiki\/Range<\/a><\/li>\n<li><a href=\"https:\/\/tour.golang.org\/moretypes\/13\" target=\"_blank\">https:\/\/tour.golang.org\/moretypes\/13<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/howDo\/archive\/2013\/06\/01\/GoLang-Control.html\" target=\"_blank\">http:\/\/www.cnblogs.com\/howDo\/archive\/2013\/06\/01\/GoLang-Control.html<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a Go\u8bed\u8a00\u4e2d\u7684for&#8230;range\u8bed\u6cd5\u5bf9\u4e8e\u521a\u63a5\u89e6\u7684\u4eba\u6765\u8bf4\u7a0d\u5fae\u6709\u90a3\u4e48\u70b9\u4e0d\u9002\u5e94\uff0c\u5bb9 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[106,563,565],"class_list":["post-2524","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-for","tag-go","tag-range"],"views":3354,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2524"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2524\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}