From: Rob Pike Date: Wed, 9 Sep 2009 00:11:57 +0000 (-0700) Subject: string range X-Git-Tag: weekly.2009-11-06~617 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c77c3b019606bdfd0e4dfa1e53ccbbd80e2f5a20;p=gostls13.git string range R=rsc DELTA=22 (19 added, 0 deleted, 3 changed) OCL=34463 CL=34463 --- diff --git a/doc/effective_go.html b/doc/effective_go.html index 740c1cf8e9..becfd17b2a 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -489,10 +489,10 @@ and while and there is no do-while. There are three forms, only one of which has semicolons:

-// Like a C for:
+// Like a C for
 for init; condition; post { }
 
-// Like a C while:
+// Like a C while
 for condition { }
 
 // Like a C for(;;)
@@ -521,10 +521,29 @@ for key, value := range m {  // key is unused; could call it '_'
 }
 
+

+For strings, the range does more of the work for you, breaking out individual +characters by parsing the UTF-8 (erroneous encodings consume one byte and produce the +replacement rune U+FFFD). The loop +

+
+for pos, char := range "日本語" {
+    fmt.Printf("character %c starts at byte position %d\n", char, pos)
+}
+
+

+prints +

+
+character 日 starts at byte position 0
+character 本 starts at byte position 3
+character 語 starts at byte position 6
+
+

Finally, since Go has no comma operator and ++ and -- are statements not expressions, if you want to run multiple variables in a for -you can use parallel assignment: +you should use parallel assignment:

 // Reverse a