How to iterate over a Map using for loop in Go?
The for…range loop statement can be used to fetch the index and element of a map.
Example
package main
import "fmt"
func main() {
var employee = map[string]int{"Mark": 10, "Sandy": 20,
"Rocky": 30, "Rajiv": 40, "Kate": 50}
for key, element := range employee {
fmt.Println("Key:", key, "=>", "Element:", element)
}
}
Output
Key: Rocky => Element: 30
Key: Rajiv => Element: 40
Key: Kate => Element: 50
Key: Mark => Element: 10
Key: Sandy => Element: 20
Most Helpful This Week
How to use array in Go Programming Language?
How to wait for Goroutines to Finish Execution?
How to import structs from another package in Go?
Pass different types of arguments in variadic function
How to read names of all files and folders in current directory?
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time