How to iterate over a map in Golang?

In this post, we are going to look at the various ways of iterating over a map.

Let’s see all of the above points one by one.

Iterating over key-value pairs

We can easily iterate over a map using the for…range loop but we should remember that the insertion order of key-value pairs won’t be maintained when we will be iterating over the map.

package main

import (
	"fmt"
)

func main() {

	theMap := make(map[string]int)

	theMap["first"] = 10
	theMap["third"] = 30
	theMap["second"] = 20

	for key, value := range theMap {
		fmt.Println("key =", key, " & value =", value)
	}

}

Output –

key = third  & value = 30
key = second  & value = 20
key = first  & value = 10

Here, we can see that the key-value pairs are printed in a different order than they were inserted.

Iterating over keys only

If we only want the keys of the map, then we can omit the value part in the for…range loop of the above program.

package main

import (
	"fmt"
)

func main() {

	theMap := make(map[string]int)

	theMap["first"] = 10
	theMap["third"] = 30
	theMap["second"] = 20

	// getting only the keys
	for key := range theMap {
		fmt.Println("key =", key)
	}

}

Output –

key = first
key = third
key = second

Iterating over values only

We can also iterate only over the values too but not in the same way as we did for keys. Here we have to use the blank identifier ( _ ) in place of the key variable within the for…range loop to ignore the key variable.

package main

import (
	"fmt"
)

func main() {

	theMap := make(map[string]int)

	theMap["first"] = 10
	theMap["third"] = 30
	theMap["second"] = 20

	// getting only the values
	for _, value := range theMap {
		fmt.Println("value =", value)
	}

}

Output –

value = 10
value = 30
value = 20

Iterating a map in an orderly fashion

Here, we have to note one point that the key-value pairs that we are adding into the map will not be iterated in the same order, instead, they will be iterated in random order. Golang maps internally use a HashMap because of which the insertion order is not maintained. So, if we want to iterate over the map in some orderly fashion, then we have to do it ourselves.

Let’s say that we want to iterate in the same order that the keys are inserted into the map, then we can have an array or a slice to keep track of the keys as they will maintain the order of the keys inserted, and then we will be able to fetch the values based on that.

package main

import (
	"fmt"
)

func main() {

	theMap := make(map[string]int)

	var storeKeys []string

	// inserting the keys, both in the map and the slice
	theMap["first"] = 10
	storeKeys = append(storeKeys, "first")

	theMap["second"] = 20
	storeKeys = append(storeKeys, "second")

	theMap["third"] = 30
	storeKeys = append(storeKeys, "third")

	theMap["fourth"] = 40
	storeKeys = append(storeKeys, "fourth")

	theMap["fifth"] = 50
	storeKeys = append(storeKeys, "fifth")

	// iterating over the map by maintaining the insertion order
	for _, key := range storeKeys {
		fmt.Println("key =", key, " & value =", theMap[key])
	}

}

Output –

key = first  & value = 10
key = second  & value = 20
key = third  & value = 30
key = fourth  & value = 40
key = fifth  & value = 50

Related Articles

Hope you liked the article. If you have any doubts or concerns, please feel free to write us in comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *