Map in Golang

Hash table and reb balk tree rocks

What is Map in C++

We also have a map which is an ordered map. The order means the key of the map is in order. The order of the key is guaranteed by a red-black tree.

What is Map in Python

What is Map in Golang[1]?

Basic

“A Go map type looks like this:

map[KeyType]ValueType

where KeyType may be any type that is comparable (more on this later), and ValueType may be any type at all, including another map!

This variable m is a map of string keys to int values:

var m map[string]int " From [1]

Do we have an ordered map in Golang?

Not quite sure yet. We do not have an ordered map in python. Maybe Golang also doesn't have it. I will update when I am sure about this part.

Key types[1]

As mentioned earlier, map keys may be of any type that is comparable. The language spec defines this precisely, but in short, comparable types are boolean, numeric, string, pointer, channel, and interface types, and structs or arrays that contain only those types. Notably absent from the list are slices, maps, and functions; these types cannot be compared using ==, and may not be used as map keys.

It’s obvious that strings, ints, and other basic types should be available as map keys, but perhaps unexpected are struct keys. Struct can be used to key data by multiple dimensions. For example, this map of maps could be used to tally web page hits by country. From [1]

From this description, it seems that the Map in golang is an ordered map.

Iteration order[1]

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. If you require a stable iteration order you must maintain a separate data structure that specifies that order. [1]

From this description, it seems that the map is not ordered.

Reference

Data Scientist/MLE/SWE @takemobi