Standard library / maps

maps.Collect()

Collects key-value pairs into a newly initialized map. Later duplicate keys replace earlier values. Expected stdout: '2\n'.

Formal Syntax & Structure

go syntaxgo
func Collect[K comparable, V any](seq iter.Seq2[K, V]) map[K]V

Parameters & Attributes

NameTypeDescription
seqiter.Seq2[K, V]Iterator function producing the input sequence.

Return Value

map[K]V

Collects key-value pairs into a newly initialized map. Later duplicate keys replace earlier values.

Working Example

Example Snippetgo
package main
import (  "fmt"  "maps")
func main() {  seq := func(yield func(string, int) bool) {    if !yield("a", 1) {      return    }    yield("a", 2)  }  fmt.Println(maps.Collect(seq)["a"])}
Go Reference