Help us shape the future of Go! Take the 2026 Go Developer Survey

collections

package
v1.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package collections provides small, type-safe slice and map helpers that the Go standard library's slices and maps packages do not cover. Prefer the stdlib directly where it suffices (slices.Sorted, slices.Contains, maps.Keys/Values, etc.); this package only adds the transformation and filtering helpers that would otherwise be hand-rolled as range loops in hundreds of handler list-builders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[T any](s []T, keep func(T) bool) []T

Filter returns a new slice containing only the elements of s for which keep returns true, preserving order. Returns nil when s is nil.

func Map

func Map[A, B any](s []A, f func(A) B) []B

Map returns a new slice holding f(v) for each element of s, preserving order. It is the type-safe replacement for the `out := make([]B, 0, len(s)); for ... { out = append(out, f(v)) }` pattern used pervasively to convert internal records into wire/response shapes.

func MapValues

func MapValues[K comparable, V, B any](m map[K]V, f func(V) B) []B

MapValues returns a slice of f(v) for each value in m. Iteration order is unspecified (map order); use SortedKeys plus a lookup when order matters.

func SortedKeys

func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K

SortedKeys returns the keys of m sorted in ascending order. It replaces the common `for k := range m { keys = append(keys, k) }; sort.Strings(keys)` idiom with a single type-safe call. Like that idiom it always returns a non-nil slice (empty for a nil or empty map), so callers that marshal the result get a JSON array rather than null.

func Values

func Values[K comparable, V any](m map[K]V) []V

Values returns the values of m as a slice in unspecified order. It is a convenience wrapper over slices.Collect(maps.Values(m)).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL