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 <...")
 
Line 8: Line 8:
</syntaxhighlight>
</syntaxhighlight>


iterating over mappings
iterating over arrays/maps
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// iterate key/values in map
// iterate key/values in map

Revision as of 20:41, 23 May 2022

For Loops

initializer/condition based loops

for i := 0; i < 10; i++ {
    sum += i
}

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)
}