| 1 |
// Package maps provides utility functions for working with maps. |
| 2 |
package maps |
| 3 |
|
| 4 |
import ( |
| 5 |
"cmp" |
| 6 |
"slices" |
| 7 |
) |
| 8 |
|
| 9 |
// SortedKeys returns the keys of the map m. |
| 10 |
// The keys will be sorted. |
| 11 |
func SortedKeys[M ~map[K]V, K cmp.Ordered, V any](m M) []K { |
| 12 |
r := Keys(m) |
| 13 |
slices.Sort(r) |
| 14 |
return r |
| 15 |
} |
| 16 |
|
| 17 |
// Keys returns the keys of the map m. |
| 18 |
func Keys[M ~map[K]V, K cmp.Ordered, V any](m M) []K { |
| 19 |
r := make([]K, 0, len(m)) |
| 20 |
for k := range m { |
| 21 |
r = append(r, k) |
| 22 |
} |
| 23 |
return r |
| 24 |
} |