Skip to content
Tutorials Center
My learning

Topics

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.

Go
m := map[string]int{"a": 1}
c := maps.Clone(m)
c["b"] = 2
fmt.Println(len(m), len(c)) // 1 2

Try it

Go Clone, Copy, Equal and DeleteFunc

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.

Go Iterators: Keys, Values, Collect, Insert

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

Go
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, Insert and Collect return or consume iter.Seq and iter.Seq2; the others work from Go 1.21.
  • Iteration order stays random. Keys and Values visit entries in the same unspecified order as a range loop. Wrap them in slices.Sorted when you need a stable order.
  • Copies are shallow. Clone copies keys and values as they are, so a map of slices or pointers still shares the inner data.
  • Equal needs comparable values. For maps of slices or structs with slices, use EqualFunc with your own comparison.
  • Copy overwrites. Keys that exist in both maps take the value from the source.
  • Deleting during iteration is safe. DeleteFunc does 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.