Iterating Maps
06/14/21 08:42 Filed in: GO
Last time I learned about maps in GO and how to create, change, and delete them. Today, I go over iterating over a map.
If I have a map of floats mapped by a string:
theMap := map[string] float64 {"1st": 1.7, "2nd" : 8.3, "3rd" 1.0 }
I can iterate the map using the syntax:
for key, value := range theMap {
}
which is similar to other languages. You can also use the underscore character to ignore either the key or value:
for _, value := range theMap { // ignore the key
}
Or you can use:
for key := range theMap { // ignore the value
}
to process just the keys.
As in other languages, GOs maps are unordered collections meaning that the items are iterated in random order.
In order (no pun intended) to loop through a map in a specific order, you can create a slice based on the map. For example, you can use the sort package.
package main
import {
"fmt"
"sort"
}
func main() {
theMap := map[string] float64 {"1st": 1.7, "2nd" : 8.3, "3rd" 1.0 }
var sorted [] float64 // create an empty slice
for key in range theMap {
sorted = append(theMap,key) // move a key from the map to the slice.
}
sort.Strings(sorted) // sort the slice of keys (strings)
for _, value := range sorted { // iterate the slice
fmt.Printf(theMap[value]) // pulling the corresponding map value
}
}
This is a pattern similar to one I might use in other languages. The one thing I'm not happy about is the first loop used to pull the keys into the slice. Most other languages have a "keys" method or property which pulls all of the keys in one step.
Those are the keys to maps. Next time, I'll start looking at structs in GO.
Iterating a Map
If I have a map of floats mapped by a string:
theMap := map[string] float64 {"1st": 1.7, "2nd" : 8.3, "3rd" 1.0 }
I can iterate the map using the syntax:
for key, value := range theMap {
}
which is similar to other languages. You can also use the underscore character to ignore either the key or value:
for _, value := range theMap { // ignore the key
}
Or you can use:
for key := range theMap { // ignore the value
}
to process just the keys.
As in other languages, GOs maps are unordered collections meaning that the items are iterated in random order.
In order (no pun intended) to loop through a map in a specific order, you can create a slice based on the map. For example, you can use the sort package.
package main
import {
"fmt"
"sort"
}
func main() {
theMap := map[string] float64 {"1st": 1.7, "2nd" : 8.3, "3rd" 1.0 }
var sorted [] float64 // create an empty slice
for key in range theMap {
sorted = append(theMap,key) // move a key from the map to the slice.
}
sort.Strings(sorted) // sort the slice of keys (strings)
for _, value := range sorted { // iterate the slice
fmt.Printf(theMap[value]) // pulling the corresponding map value
}
}
This is a pattern similar to one I might use in other languages. The one thing I'm not happy about is the first loop used to pull the keys into the slice. Most other languages have a "keys" method or property which pulls all of the keys in one step.
Those are the keys to maps. Next time, I'll start looking at structs in GO.