smegmesh/pkg/lib/conv.go
2023-10-20 12:41:06 +01:00

23 lines
430 B
Go

package lib
// MapToSlice converts a map to a slice in go
func MapValues[K comparable, V any](m map[K]V) []V {
return MapValuesWithExclude(m, map[K]struct{}{})
}
func MapValuesWithExclude[K comparable, V any](m map[K]V, exclude map[K]struct{}) []V {
values := make([]V, len(m)-len(exclude))
i := 0
for k, v := range m {
if _, excluded := exclude[k]; excluded {
continue
}
values[i] = v
i++
}
return values
}