Golang loops: Difference between revisions
From wikinotes
(Created page with "= For Loops = <blockquote> initializer/condition based loops <syntaxhighlight lang="go"> for i := 0; i < 10; i++ { sum += i } </syntaxhighlight> iterating over mappings <...") |
No edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Breaking, Continuing, etc. = | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
break // exit loop without finishing | |||
continue // skip to next iteration | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="go"> | |||
for i := 0; i < 10; i++ { | |||
// skip to next iteration | |||
if i % 2 { | |||
continue | |||
} | |||
// exit loop immediately | |||
if i == 5 { | |||
break | |||
} | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Breaking, Continuing, etc. --> | |||
= For Loops = | = For Loops = | ||
<blockquote> | |||
== Infinite == | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
for { | |||
// ... | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Infinite --> | |||
== Initializer / Condition == | |||
<blockquote> | <blockquote> | ||
initializer/condition based loops | initializer/condition based loops | ||
Line 8: | Line 41: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
iterating over | initializer/condition based loops with multiple variables | ||
<syntaxhighlight lang="go"> | |||
for i, j :- 0, 0; i < 10; i, j = i+1, j+1 { | |||
// ... | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- initializer/condition --> | |||
== Over condition == | |||
<blockquote> | |||
<syntaxhighlight lang="go"> | |||
i := 0 | |||
for i < 5 { | |||
i++ | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- condition --> | |||
== Over arrays/maps == | |||
<blockquote> | |||
iterating over arrays/maps | |||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
// iterate key/values in map | // iterate key/values in map | ||
Line 25: | Line 78: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- over arrays/maps --> | |||
== Labels == | |||
<blockquote> | |||
Go has a GOTO-like syntax for supporting breaking out of complex logic. | |||
<syntaxhighlight lang="go"> | |||
func main() { | |||
Loop: // <-- label, can be any word | |||
for i < 5 { | |||
for j < 5 { | |||
if i == 3 && j == 3 { | |||
break Loop // <-- breaks out of all loops defined under label 'Loop' | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Labels --> | |||
</blockquote><!-- For Loops --> | </blockquote><!-- For Loops --> |
Latest revision as of 23:15, 6 June 2022
Breaking, Continuing, etc.
break // exit loop without finishing continue // skip to next iterationfor i := 0; i < 10; i++ { // skip to next iteration if i % 2 { continue } // exit loop immediately if i == 5 { break } }
For Loops
Infinite
for { // ... }Initializer / Condition
initializer/condition based loops
for i := 0; i < 10; i++ { sum += i }initializer/condition based loops with multiple variables
for i, j :- 0, 0; i < 10; i, j = i+1, j+1 { // ... }Over condition
i := 0 for i < 5 { i++ }Over arrays/maps
iterating over arrays/maps
// iterate key/values in map for key, value := range oldMap { fmt.Println(key + "=" + value) } // iterate only keys in map (simply don't assign val) for key := range oldMap { fmt.Println(key) } // iterate only values in map for _, value := range oldMap { fmt.Println(value) }Labels
Go has a GOTO-like syntax for supporting breaking out of complex logic.
func main() { Loop: // <-- label, can be any word for i < 5 { for j < 5 { if i == 3 && j == 3 { break Loop // <-- breaks out of all loops defined under label 'Loop' } } } }