GOLANG

The anatomy of maps in Go

A map is a composite data type that can hold data represented by key:value pairs.

Uday Hiwarale
RunGo
Published in
6 min readSep 27, 2018

--

(source: pexels.com)

☛ What is a map?

A map is like an array except, instead of an integer index, you can have string or any other data types as a key. An illustration of a map looks like

{
stringKey: intValue,
stringKey: intValue
...
}

The syntax to define a map is

var myMap map[keyType]valueType

Where keyType is the data type of map keys while valueType is the data type of map values. A map is a composite data type because it is composed of primitive data types (see variables lesson for primitive data types).

Let’s declare a simple map.

https://play.golang.org/p/Rv6dEDtbsoZ

In the above program, we have declared a map m which is empty, because zero-value of a map is nil. But the thing about nil map is, we can’t add values to it because like slices, map cannot hold any data, rather they reference the internal data structure that holds the data.

--

--