Avatar ·

How to iterate over all map keys in Go? Iteration over dictionary

Hello everyone! I just started learning Go and encountered a problem when working with maps. I need to go through all the keys of the dictionary and perform some actions with them. I'm used to Python, where you can just write `for key in my_dict:`, but this doesn't work in Go. I have a map that stores user names and their ages: ```go ages := map[string]int{ "Alice": 30, "Bob": 25, "Charlie": 35, } ``` I want to display all users who are older than 28. I tried to do it like this: ```go for key := range ages { if ages[key] > 28 { fmt.Println(key) } } ``` But I'm not sure this is the correct way. First, I read that the order of keys in a map is not guaranteed — this confuses me a bit. Second, what if I need to get the n