Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
c77c3b01
Commit
c77c3b01
authored
Sep 08, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
string range
R=rsc DELTA=22 (19 added, 0 deleted, 3 changed) OCL=34463 CL=34463
parent
32aa5be6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
3 deletions
+22
-3
doc/effective_go.html
doc/effective_go.html
+22
-3
No files found.
doc/effective_go.html
View file @
c77c3b01
...
...
@@ -489,10 +489,10 @@ and <code>while</code> and there is no <code>do-while</code>.
There are three forms, only one of which has semicolons:
</p>
<pre>
// 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 '_'
}
</pre>
<p>
For strings, the
<code>
range
</code>
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
</p>
<pre>
for pos, char := range "日本語" {
fmt.Printf("character %c starts at byte position %d\n", char, pos)
}
</pre>
<p>
prints
</p>
<pre>
character 日 starts at byte position 0
character 本 starts at byte position 3
character 語 starts at byte position 6
</pre>
<p>
Finally, since Go has no comma operator and
<code>
++
</code>
and
<code>
--
</code>
are statements not expressions, if you want to run multiple variables in a
<code>
for
</code>
you
can
use parallel assignment:
you
should
use parallel assignment:
</p>
<pre>
// Reverse a
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment