Standard library / maps

maps.Keys()

Yields keys in unspecified order. Collect and sort when deterministic order is needed. Expected stdout: '[a b]\n'.

Formal Syntax & Structure

go syntaxgo
func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K]

Parameters & Attributes

NameTypeDescription
mMapInput map.

Return Value

iter.Seq[K]

Yields keys in unspecified order. Collect and sort when deterministic order is needed.

Working Example

Example Snippetgo
package main
import (  "fmt"  "maps"  "slices")
func main() {  fmt.Println(slices.Sorted(maps.Keys(map[string]int{"b": 2, "a": 1})))}
Go Reference