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


Deletion of map elements

Deletion of map elements The built-in function delete removes the element with key k from a map m. The value k must be assignable to the key type of m. delete(m, k) // remove element m[k] from map m We already looked at clear, which deletes everything in a map. delete lets you surgically remove individual keys from a map. Unlike some languages, which give you the means to return an element as it’s deleted, if you want to do that in Go, you’ll need to do it in two steps:


Making slices, maps and channels

Making slices, maps and channels The built-in function make takes a type T, optionally followed by a type-specific list of expressions. The core type of T must be a slice, map or channel. It returns a value of type T (not *T). The memory is initialized as described in the section on initial values. Call Core type Result make(T, n) slice slice of type T with length n and capacity n make(T, n, m) slice slice of type T with length n and capacity m make(T) map map of type T make(T, n) map map of type T with initial space for approximately n elements make(T) channel unbuffered channel of type T make(T, n) channel buffered channel of type T, buffer size n The only time you absolutely need to use make is when creating channels.


Clear

Clear The built-in function clear takes an argument of map, slice, or type parameter type, and deletes or zeroes out all elements [[Go 1.21(https://go.dev/ref/spec#Go_1.21)]]. Call Argument type Result clear(m) map[K]T deletes all entries, resulting in an empty map (len(m) == 0) clear(s) []T sets all elements up to the length of s to the zero value of T clear(t) type parameter see below clear is a relatively recent addition to Go, added just over a year ago.

Get daily content like this in your inbox!

Subscribe