notexOPENOverview
Notex/Languages/Maps
01.19LESSON 19 OF 33 · 2 MIN READ

Maps

Always free to read

Slices are useful when you have sequential data. Like most languages, Go provides a built-in data type for situations where you want to associate one value to another. The map type is written as map[keyType]valueType. Let’s take a look at a few ways to declare maps. First, you can use a var declaration to create a map variable that’s set to its zero value:

var nilMap map[string]int

In this case, nilMap is declared to be a map with string keys and int values. The zero value for a map is nil. A nil map has a length of 0. Attempting to read a nil map always returns the zero value for the map’s value type. However, attempting to write to a nil map variable causes a panic.

You can use a:= declaration to create a map variable by assigning it a map literal:

totalWins := map[string]int{}

In this case, you are using an empty map literal. This is not the same as a nil map. It has a length of 0, but you can read and write to a map assigned an empty map literal. Here’s what a nonempty map literal looks like:

teams := map[string][]string {
    "Orcas": []string{"Fred", "Ralph", "Bijou"},
    "Lions": []string{"Sarah", "Peter", "Billie"},
    "Kittens": []string{"Waldo", "Raul", "Ze"},
}

A map literal’s body is written as the key, followed by a colon (:), then the value. A comma separates each key-value pair in the map, even on the last line. In this example, the value is a slice of strings. The type of the value in a map can be anything. There are some restrictions on the types of the keys that I’ll discuss in a bit.

If you know how many key-value pairs you intend to put in the map but don’t know the exact values, you can use make to create a map with a default size:

ages := make(map[int][]string, 10)

Maps created with make still have a length of 0, and they can grow past the initially specified size.

Maps are like slices in several ways:

  • Maps automatically grow as you add key-value pairs to them.

  • If you know how many key-value pairs you plan to insert into a map, you can use make to create a map with a specific initial size.

  • Passing a map to the len function tells you the number of key-value pairs in a map.

  • The zero value for a map is nil.

  • Maps are not comparable. You can check if they are equal to nil, but you cannot check if two maps have identical keys and values using == or differ using !=.

  • The key for a map can be any comparable type. This means you cannot use a slice or a map as the key for a map.

Tip

When should you use a map, and when should you use a slice? You should use slices for lists of data when the data should be processed sequentially or the order of the elements is important.

Maps are useful when you need to organize values using something other than an increasing integer value, such as a name.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K