Go Maps Iterating the keys of a map

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

people := map[string]int{
  "john": 30,
  "jane": 29,
  "mark": 11,
}

for key, _ := range people {
  fmt.Println("Name:", key)
}

If you are just looking for the keys, since they are the first value, you can simply drop the underscore:

for key := range people {
  fmt.Println("Name:", key)
}

Note that when iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next.



Got any Go Question?