Index expressions for maps

November 15, 2023

Index expressions

For a of map type M:

  • x’s type must be assignable to the key type of M

No surprise here. The key must be assignable to the key type. But notably, it needn’t be of an identical type, so long as it is assignable to the key type.

m := map[string]int{
	"foo": 3,
	"bar": 12,
}

m[string("foo")] // string("foo") is explicitly of type string, so assignable.
m["foo"] // Untyped constant string "foo" is assignable to type `string`

type Key string
k := Key("bar")
m[k] // k is of type Key, not assignable to type string without explicit conversion
  • if the map contains an entry with key x, a[x] is the map element with key x and the type of a[x] is the element type of M

This should be intuitive.

v := m["bar"] // v has value 12, and is of type int
  • if the map is nil or does not contain such an entry, a[x] is the zero value for the element type of M

An empty map (eg map[string]int{}) and a nil map behave the same when attempting to reference values stored within. And whether empty, nil, or the requested key simply doesn’t exist in a populated map, the result is the zero value of the map’s element type:

m := map[string]int{"foo":123}
n := map[string]int{}
var o map[string]int // nil map

fmt.Println(m["x"], n["x"], o["x"]) // Prints: 0 0 0

Quotes from The Go Programming Language Specification Version of August 2, 2023


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 <.