Using maps

May 12, 2023

Today I’m jumping around a bit, for the sake of coherence.

Map types

… [Map] elements may be added during execution using assignments and retrieved with index expressions; they may be removed with the delete built-in function.

A new, empty map value is made using the built-in function make, …

Let’s summarize with examples. We’ll dive into details as the relevant portions of the spec come up.

To create a map, you use the built-in function make:

x := make(map[string]string)  // x is an empty map of string to string

Not mentioned in this section, you can also create a map with a composite literal expression:

x := map[string]string{} // x is an empty map of string to string
y := map[string]string{  // y is a map of string to string with two key/value pairs
  "cow": "moo",
  "pig": "oink",

To read a map value, you use an index expression:

sound := y["cow"] // sound equals "moo"

To assign a value (or re-assign, if the key already exists), you use an assignment statement:

y["duck"] = "quack" // y now contains three key/value pairs

And to delete a value, you use the built-in delete function:

delete(y, "pig") // y now contains two key/value pairs

And finally one last usability note:

… A nil map is equivalent to an empty map except that no elements may be added.

A nil map can be created by declaring a variable without an assignment:

var z map[string]string // z is a nil map of string to string
z["foo"] = "bar"        // Runtime panic: assignment to entry in nil map

Quotes from The Go Programming Language Specification Version of December 15, 2022

Share this

Related Content

Type identities of compound types

So we now know that types are either identical or different. let’s look at the details, with examples. Type identity … In detail: Two array types are identical if they have identical element types and the same array length. So given: type ( X = [10]int Y = X Z [10]int ) Types X, Y, and [10]int are identical to each other, but not to [9]int or [10]int32. Type Z is also different from all the others, due to the fact that it is a named type.

Creating channels

Channel types … A new, initialized channel value can be made using the built-in function make, which takes the channel type and an optional capacity as arguments: make(chan int, 100) Here we have the semi-magical make built-in function again. We’ve already seen it for use with arrays and slices. Consider this yet another clue that a channel is just another type, similar to array and slice, in that it “just” holds a bunch of values, and isn’t magical in any way.

Map length and capacity

Maps, like arrays and slices, have length. Map types … The number of map elements is called its length. For a map m, it can be discovered using the built-in function len and may change during execution. … This should be straight forward, but let’s consider a couple examples for good measure: x := map[string]int{"a": 1, "b": 2, "c": 3} fmt.Println(len(x)) // 3 var y map[string]float64 fmt.Println(len(y)) // 0 And somewhat deceptively, their capacity can also be set, or at least hinted at, but not read.