Go Reference ยท Collections: sort, slices, maps
Go maps package
Reference for the Go maps package (Go 1.21+): Clone, Copy, Equal, DeleteFunc, plus the Go 1.23 iterator functions Keys, Values, All, Collect, Insert.
The package maps holds generic helpers for maps of any key and value types. It arrived in Go 1.21 with functions to clone, copy, compare and filter maps, and Go 1.23 added iterator-based functions that turn a map into a sequence and back.
Before this package, even a simple map copy needed a hand-written loop. The functions are generic over the map type M ~map[K]V, so they accept named map types too.
m := map[string]int{"a": 1} c := maps.Clone(m) c["b"] = 2 fmt.Println(len(m), len(c)) // 1 2
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func Clone[M ~map[K]V, K comparable, V any](m M) M func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K] func Values[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[V] func All[Map ~map[K]V, K comparable, V any](m Map) iter.Seq2[K, V] func Insert[Map ~map[K]V, K comparable, V any](m Map, seq iter.Seq2[K, V]) func Collect[K comparable, V any](seq iter.Seq2[K, V]) map[K]V
| Name | Description |
|---|---|
m, dst, src |
The maps to read or change. A nil map is treated as empty when reading. |
del |
Called with each key and value; return true to delete the entry. |
eq |
Compares two values for EqualFunc. |
seq |
A sequence of key-value pairs (iter.Seq2), consumed by Insert and Collect. |
Returns: Clone returns a shallow copy (nil stays nil). Equal and EqualFunc return a bool. Keys, Values and All return iterators. Collect returns a new map.
Edge cases: Copy and Insert panic if the destination is a nil map and there is something to add, just like a normal assignment would.
Usage notes
- Iterators need Go 1.23.
Keys,Values,All,InsertandCollectreturn or consumeiter.Seqanditer.Seq2; the others work from Go 1.21. - Iteration order stays random.
KeysandValuesvisit entries in the same unspecified order as arangeloop. Wrap them inslices.Sortedwhen you need a stable order. - Copies are shallow.
Clonecopies keys and values as they are, so a map of slices or pointers still shares the inner data. Equalneeds comparable values. For maps of slices or structs with slices, useEqualFuncwith your own comparison.Copyoverwrites. Keys that exist in both maps take the value from the source.- Deleting during iteration is safe.
DeleteFuncdoes exactly that and is the clearest way to filter a map in place. - Not safe for concurrent use. Like plain maps, none of these functions lock; see Maps and concurrency.