ZetCode

Golang maps.All

last modified June 29, 2026

This tutorial explains how to use the maps.All function in Go. We cover iterating over map entries with practical examples.

The maps.All function returns an iterator (iter.Seq2[K, V]) over all key-value pairs of a map. It was added to the standard maps package in Go 1.23.

The iterator can be consumed with a for range loop or passed to functions that accept an iter.Seq2 value.

Basic maps.All Example

maps.All returns an iter.Seq2[K, V], which is a first-class value. Assigning it to a typed variable makes the iterator type explicit and shows that it can be stored, passed, or deferred.

basic_all.go
package main

import (
    "fmt"
    "iter"
    "maps"
)

func main() {
    scores := map[string]int{
        "Alice": 85,
        "Bob":   92,
        "Carol": 78,
    }

    var it iter.Seq2[string, int] = maps.All(scores)

    for k, v := range it {
        fmt.Printf("%s: %d\n", k, v)
    }
}

maps.All(scores) produces the iterator; assigning it to it of type iter.Seq2[string, int] confirms that the return type is a concrete iterator value, not a special map construct.

Filtering Entries During Iteration

Because maps.All returns an iter.Seq2, it can be passed to a generic filter adapter. The adapter wraps the iterator and yields only the pairs that satisfy the predicate.

filter_entries.go
package main

import (
    "fmt"
    "iter"
    "maps"
)

func filterSeq[K, V any](seq iter.Seq2[K, V], pred func(K, V) bool) iter.Seq2[K, V] {
    return func(yield func(K, V) bool) {
        for k, v := range seq {
            if pred(k, v) {
                if !yield(k, v) {
                    return
                }
            }
        }
    }
}

func main() {
    scores := map[string]int{
        "Alice": 85,
        "Bob":   55,
        "Carol": 78,
        "Dave":  42,
    }

    passing := filterSeq(maps.All(scores), func(_ string, v int) bool {
        return v >= 60
    })

    fmt.Println("Passing scores (>= 60):")
    for name, score := range passing {
        fmt.Printf("  %s: %d\n", name, score)
    }
}

filterSeq accepts any iter.Seq2 and returns a new lazy iterator. We feed it maps.All(scores) and a predicate; the resulting passing iterator only yields pairs where the score is at least 60.

Passing the Iterator to a Function

maps.All returns an iter.Seq2[K, V] value that can be passed directly to functions. This example sums all values by accepting the iterator as a parameter.

iter_func.go
package main

import (
    "fmt"
    "iter"
    "maps"
)

func sumValues(seq iter.Seq2[string, int]) int {
    total := 0
    for _, v := range seq {
        total += v
    }
    return total
}

func main() {
    prices := map[string]int{
        "apple":  5,
        "banana": 3,
        "cherry": 8,
    }

    fmt.Println("Total:", sumValues(maps.All(prices)))
}

The sumValues function is decoupled from the map type; it works with any iter.Seq2[string, int] source. We pass maps.All(prices) directly as the argument.

Collecting the Iterator into a New Map

The companion function maps.Collect converts an iter.Seq2 back into a map. Together with maps.All this provides a concise way to copy a map.

collect.go
package main

import (
    "fmt"
    "maps"
)

func main() {
    original := map[string]int{
        "a": 1,
        "b": 2,
        "c": 3,
    }

    clone := maps.Collect(maps.All(original))

    clone["d"] = 4
    fmt.Println("original:", original)
    fmt.Println("clone:   ", clone)
}

maps.All produces the iterator and maps.Collect drains it into a fresh map. Mutating clone does not affect original.

Working with Custom Value Types

maps.All works with any map type, including maps with struct values. Here we pass the iterator to a dedicated function that counts out-of-stock items, keeping the logic independent of the map type.

custom_types.go
package main

import (
    "fmt"
    "iter"
    "maps"
)

type Product struct {
    Name     string
    InStock  bool
    Quantity int
}

func countOutOfStock(seq iter.Seq2[string, Product]) (int, []string) {
    var names []string
    for _, p := range seq {
        if !p.InStock {
            names = append(names, p.Name)
        }
    }
    return len(names), names
}

func main() {
    inventory := map[string]Product{
        "p1": {"Laptop", true, 10},
        "p2": {"Mouse", false, 0},
        "p3": {"Keyboard", true, 15},
    }

    n, names := countOutOfStock(maps.All(inventory))
    fmt.Printf("%d product(s) out of stock: %v\n", n, names)
}

countOutOfStock accepts iter.Seq2[string, Product], so it is not tied to a map — any source that produces the same iterator type works. maps.All(inventory) converts the map into that iterator at the call site.

Checking a Condition Across All Entries

To test whether all entries satisfy a predicate, iterate with maps.All and return early on the first failure. This is the idiomatic Go pattern.

all_positive.go
package main

import (
    "fmt"
    "maps"
)

func allPositive(m map[string]int) bool {
    for _, v := range maps.All(m) {
        if v <= 0 {
            return false
        }
    }
    return true
}

func main() {
    scores := map[string]int{
        "Alice": 85,
        "Bob":   92,
        "Carol": 78,
    }

    fmt.Println("All positive:", allPositive(scores))

    mixed := map[string]int{
        "Alice": 85,
        "Bob":   -5,
    }
    fmt.Println("All positive:", allPositive(mixed))
}

The helper returns false as soon as a non-positive value is found, mimicking short-circuit evaluation. The empty-map case naturally returns true because the loop body never executes.

Iterating Over an Empty Map

Ranging over maps.All on a nil or empty map is safe and simply performs zero iterations.

empty_map.go
package main

import (
    "fmt"
    "maps"
)

func main() {
    var empty map[string]int

    count := 0
    for range maps.All(empty) {
        count++
    }

    fmt.Println("Iterations over nil map:", count)
}

The loop body is never reached, so count stays zero. No nil-pointer panic occurs because maps.All handles nil maps safely.

Source

Go maps.All documentation

This tutorial covered the maps.All function in Go 1.23. It returns an iter.Seq2[K, V] iterator that can be ranged over or passed to functions accepting iterators.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Go tutorials.