Skip to content

Archive

Maps

14 articles
Go 16 Sep 2026 4 min read

Go Map Iteration Order Is Not Stable

A range over a Go map can visit the same entries in a different order on consecutive iterations. The language specification leaves map iteration order unspecified and gives no guarantee that a later pass over an unchanged map will repeat an earlier sequence. That contract is stronger than saying that maps are merely unsorted. An unsorted container could still expose a stable insertion-dependent or storage-dependent sequence. A Go program cannot assign such meaning to map traversal.

Go 12 Sep 2026 4 min read

Merge Map Entries with maps.Copy in Go

Merging two Go maps often comes down to one precise rule: entries from one map are assigned into another, and matching keys replace existing destination values. maps.Copy gives that operation a standard-library form without changing its underlying assignment semantics. The function mutates the destination map. It does not allocate a replacement, return a merged value, or recursively copy data stored behind pointers, slices, maps, or other reference-bearing values. Source entries overwrite matching destination keys maps.Copy accepts a destination followed by a source. Every source pair is assigned to the destination. Keys that exist only in the destination remain present.

Go 12 Sep 2026 4 min read

Filter Map Entries in Place with maps.DeleteFunc

Filtering a Go map often means removing entries from an existing map rather than allocating a replacement. maps.DeleteFunc expresses that operation directly: it visits entries and deletes each pair for which a predicate returns true. That mutation model is the central detail. The function does not return a filtered copy, and callers that share the same map observe the deletions. The predicate receives both key and value maps.DeleteFunc accepts a map and a function with the key and value types of that map. A compact filter can use either argument or both:

Go 12 Sep 2026 5 min read

Compare Map Values with maps.EqualFunc in Go

Two Go maps can represent the same logical data even when their value types differ or their values need domain-specific comparison. maps.EqualFunc handles that case by matching keys normally while delegating value comparison to a caller-supplied function. That split matters. The comparator controls value equivalence only. It cannot redefine key identity, compensate for a missing key, or make maps with different entry counts equal. Key membership is checked before value equivalence The function accepts two maps with the same key type but potentially different value types:

Go 12 Sep 2026 4 min read

Clone Go Maps with maps.Clone

A Go map variable refers to mutable map state. Assigning that variable to another variable does not create an independent map: writes through either name affect the same map. When code needs a separate top-level map with the same entries, maps.Clone makes that boundary explicit. The function is deliberately narrow. It copies map entries using ordinary assignment. The resulting map can be changed independently at the key-value entry level, but reference-like data stored inside keys or values can still share underlying state.

Go 11 Sep 2026 5 min read

Iterate Go Map Values with maps.Values

Sometimes a Go map’s keys are irrelevant to the next operation. You may need to total counters, inspect status values, or pass the values into an iterator-aware helper. A plain map range works well inside one loop, but it doesn’t give you a value sequence that can cross an API boundary. Since Go 1.23, maps.Values returns an iter.Seq over a map’s values. That lets code consume values directly and postpone slice allocation until a later operation actually needs a slice.

Go 11 Sep 2026 4 min read

Iterate Go Map Pairs with maps.All

A normal range loop is often the clearest way to walk through a Go map. The situation changes when another API expects an iterator rather than a map. Starting in Go 1.23, maps.All provides that bridge by exposing a map’s key-value pairs as an iter.Seq2. maps.All doesn’t copy the map into a slice or build a second map. It returns an iterator that can feed a range loop or another iterator-aware function. The main constraint is familiar from map iteration: pair order is unspecified.

Go 11 Sep 2026 5 min read

Iterate Go Map Keys with maps.Keys

Sometimes you need only the keys from a Go map. A plain range loop handles that case well, but an iterator becomes useful when the keys need to flow into another iterator-aware API or when a function should expose keys without first allocating a slice. Since Go 1.23, maps.Keys returns an iter.Seq over a map’s keys. You can range over it directly, stop early, or pass it to helpers such as slices.Sorted and slices.Collect.

Go 11 Sep 2026 4 min read

Copy Entries Between Go Maps with maps.Copy

Sometimes you already have a destination map and need to add another map’s entries without writing a loop. Go’s maps.Copy does exactly that: it copies every key-value pair from a source map into an existing destination map. The operation is deliberately simple. Existing destination entries remain when their keys aren’t present in the source. When both maps contain the same key, the source value replaces the destination value. That makes maps.Copy useful for applying defaults, overrides, accumulated state, and other map-to-map merges where replacement is the intended conflict rule.

Go 11 Sep 2026 6 min read

Compare Go Maps with Custom Value Rules Using maps.EqualFunc

Two maps can represent the same application state even when their values aren’t directly comparable with ==. One map might hold structs containing slices, another might use a different value type, or a text field might be considered equal regardless of letter case. maps.EqualFunc handles that situation by keeping key comparison fixed while letting you define value equality. It is a compact option when the question is strictly whether two maps contain the same keys and equivalent values.

Go 10 Sep 2026 6 min read

Insert Iterator Pairs into Go Maps with maps.Insert

Sometimes a Go pipeline naturally produces key-value pairs, but the destination is a map you already have. Turning those pairs into a temporary map just to merge it adds a step that doesn’t help. Go 1.23 added maps.Insert for this case. It consumes an iter.Seq2[K, V] and writes each pair into an existing map. Existing keys are overwritten, unrelated entries stay in place, and the iterator can produce values lazily.

Go 10 Sep 2026 5 min read

Filter Go Maps In Place with maps.DeleteFunc

Removing map entries by a condition is common when cleaning caches, pruning stale state, or dropping records that no longer belong in a working set. A for range loop with delete works, but when the operation is simply “delete every entry matching this predicate,” maps.DeleteFunc states that intent directly. The function mutates the map you pass to it. That makes it a good fit for owned, mutable state, but a poor fit when callers still need the original contents.

Go 10 Sep 2026 8 min read

Copy Go Maps with maps.Clone Without Sharing Top-Level State

Copying a Go map is easy to get subtly wrong. Assigning one map variable to another doesn’t duplicate the map, so a write through either variable changes the same underlying map. Since Go 1.21, the standard library’s maps.Clone function gives you a concise way to make a separate top-level map. There is one boundary worth understanding before using it: maps.Clone is a shallow clone. Adding, deleting, or replacing entries in the clone won’t change the original map, but nested maps, slices, pointers, and other reference-bearing values can still refer to the same underlying data.

Go 10 Sep 2026 7 min read

Build Go Maps from Iterators with maps.Collect

An iterator is a convenient way to produce key-value pairs without deciding up front where they’ll be stored. Eventually, though, an API may need an ordinary map. Writing the collection loop yourself is straightforward, but Go 1.23 gives that boundary a standard name: maps.Collect. maps.Collect consumes an iter.Seq2, stores each yielded pair in a newly allocated map, and returns that map. It’s a small helper, but it makes ownership clear: the sequence produces data; Collect creates the destination.