4 ms·
I started to write one in the "Go 2 playground", but then realized they had that exact example in the draft design: func Keys(type K comparable, V interface
by benhoyt 6y ago
I started to write one in the "Go 2 playground", but then realized they had that exact example in the draft design:
func Keys(type K comparable, V interface{})(m map[K]V) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
Usage:
m := map[int]int{1:2, 2:4}
k := maps.Keys(m)
// Now k is either []int{1, 2} or []int{2, 1}.