Menu

1 / 2

Data Structures in Go[Maps]

DEV Community: go·Adetola Esther Aderibigbe·10 days ago
#N7Kah5z8
#devtogo#devto#dev#userages#value
Reading 0:00
15s threshold

Introduction to Maps

A map is a built in Go data structure that stores information as key value pairs .

Unlike arrays and slices, where elements are accessed using their positions (indexes), values in a map are accessed using their keys.

Every entry in a Go map consists of two things:

Key: An identifier used to find an entry in the map.

Value: The information associated with that key.

For example, imagine storing information about a user. We could use the user's name and email as key value pairs.

user := map [ string ] string { "name" : "Adetola" , "email" : "[email protected]" , }

Enter fullscreen mode

Exit fullscreen mode

syntax

The general syntax for declaring a map is:

map [ keyType ] valueType

Enter fullscreen mode

Exit fullscreen mode

In our example:

map [ string ] string

Enter fullscreen mode

Exit fullscreen mode

map tells Go that we are declaring a map.

The first string specifies the data type of the keys.

The second string specifies the data type of the values.

This means we…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.