Index expression special cases for maps

November 17, 2023

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:

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. The value of ok is true if the key x is present in the map, and false otherwise.

This is particularly useful to distinguish between an existing value in a map that happens to be set to the zero value. Consider:

m := map[int]string{
  1: "foo",
  2: "",
}

x := m[1] // x == "foo"
y := m[2] // y == ""
z := m[3] // z == "", even though key 3 does not exist in the map

yy, ok := m[2] // y == "", ok == true
zz, ok := m[3] // z == "", ok == false

Assigning to an element of a nil map causes a run-time panic.

So while reading from a nil map is permitted, assignment to one is not.

var m map[int]int // m is of type map[int]int, but nil
x := m[3] // x == 0, but the expression is valid
m[3] = 12 // run-time panic

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


Index expressions for maps

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.

Get daily content like this in your inbox!

Subscribe