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

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Unordered evaluation

Yesterday’s spec quote included a sentence we breezed over. Today we’re going to dig in a bit more: Order of Evaluation … the order of those events compared to the evaluation and indexing of x and the evaluation of y and z is not specified, except as required lexically. What exactly does that mean? Well, the example that follows in the spec makes it a bit more clear, but in a nutshell, the order of evaluation for index expressions is not defined.


Index expression special cases for maps

Today we’re finishing up our discussion of… Index expressions … So far, we’ve discussed what index expressions are, and specific rules as they apply to: arrays slices and strings maps type parameters Otherwise a[x] is illegal. But there are two more special cases to consider for maps: An index expression on a map a of type map[K]V used in an assignment statement or initialization of the special form v, ok = a[x] v, ok := a[x] var v, ok = a[x] yields an additional untyped boolean value.


Index expressions for type parameters

Index expressions … For a of type parameter type P: So just a reminder what this means: We’re talking about index expressions for different data types. And we’re going through different interpretations of the expression a[x]. In this example, a is of a “type parameter” type, represented by P. And as a further reminder, a type parameter looks something like [X any] or [Y ~int]. So for this example, we’re assuming that we have [P <.