Site icon XtraCode.me

Golang maps

Golang maps

Map is an unordered collection of key-value pairs where each key is unique and implements hash-table data structure that offer fast lookups, adds and deletes. They are also called associative arrays or dictionaries.

They are perfert for searching values. If given the key, the associated value can be retrieved very quickly.

Creating map

A map can be created by several ways.

Declaring a variable of type map:

Using make keyword:

The value of an uninitialized map is nil.

The key type can be any type for which the operations == and != are defined, like string, int, and float

Add element to map

Output:

Iteration over map

Remember that order of map elements is not specified. It means that result iteration over map is not guranteed to be the same.

Delete an element with a key of map

Deleting an element of the map is done with the delete() function.

Output:

Exit mobile version