collections

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](slice []T, predicate func(T) bool) bool

All checks if all elements match the predicate

func AllMap

func AllMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) bool

All checks if all key-value pairs match the predicate

func Any

func Any[T any](slice []T, predicate func(T) bool) bool

Any checks if any element matches the predicate

func AnyMap

func AnyMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) bool

Any checks if any key-value pair matches the predicate

func Chunk

func Chunk[T any](slice []T, size int) [][]T

Chunk splits slice into chunks of specified size

func Clone

func Clone[T any](slice []T) []T

Clone creates a shallow copy of the slice

func CloneMap

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

Clone creates a shallow copy of the map

func Contains

func Contains[T comparable](slice []T, element T) bool

Contains checks if a slice contains a specific element

func ContainsAny

func ContainsAny[T comparable](slice []T, elements ...T) bool

ContainsAny checks if a slice contains any of the given elements

func Count

func Count[T any](slice []T, predicate func(T) bool) int

Count counts elements that match the predicate

func CountBy

func CountBy[T any, K comparable](slice []T, keyFunc func(T) K) map[K]int

CountBy counts slice elements by the result of the key function

func Difference

func Difference[T comparable](slice1, slice2 []T) []T

Difference returns elements in slice1 that are not in slice2

func Filter

func Filter[T any](slice []T, predicate func(T) bool) []T

Filter returns a new slice containing only elements that match the predicate

func FilterMap

func FilterMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) map[K]V

Filter returns a new map containing only key-value pairs that match the predicate

func Find

func Find[T any](slice []T, predicate func(T) bool) (T, bool)

Find returns the first element that matches the predicate

func FindInMap

func FindInMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) (K, V, bool)

Find returns the first key-value pair that matches the predicate

func FindIndex

func FindIndex[T any](slice []T, predicate func(T) bool) int

FindIndex returns the index of the first element that matches the predicate

func Flatten

func Flatten[T any](slices [][]T) []T

Flatten flattens a slice of slices into a single slice

func ForEach

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

ForEach executes a function for each key-value pair

func FromSlice

func FromSlice[K comparable, V any](slice []KeyValue[K, V]) map[K]V

FromSlice creates map from slice of key-value pairs

func GetOrCreate

func GetOrCreate[K comparable, V any](m map[K]V, key K, defaultValue V) V

GetOrCreate returns the value for key, or creates and returns defaultValue if key doesn't exist

func GetOrDefault

func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V

GetOrDefault returns the value for key, or defaultValue if key doesn't exist

func GroupBy

func GroupBy[T any, K comparable](slice []T, keyFunc func(T) K) map[K][]T

GroupBy groups slice elements by the result of the key function

func HasKey

func HasKey[K comparable, V any](m map[K]V, key K) bool

HasKey checks if map contains the specified key

func HasValue

func HasValue[K comparable, V comparable](m map[K]V, value V) bool

HasValue checks if map contains the specified value

func IndexOf

func IndexOf[T comparable](slice []T, element T) int

IndexOf returns the index of the first occurrence of element in slice

func Insert

func Insert[T any](slice []T, index int, element T) ([]T, error)

Insert inserts element at the specified index

func InterfaceToMap

func InterfaceToMap[K comparable, V any](m map[string]interface{}, keyConverter func(string) (K, error)) (map[K]V, error)

InterfaceToMap converts map[string]interface{} to a typed map (with type checking)

func InterfaceToSlice

func InterfaceToSlice[T any](interfaces []interface{}) ([]T, error)

InterfaceToSlice converts []interface{} to a typed slice (with type checking)

func Intersection

func Intersection[T comparable](slice1, slice2 []T) []T

Intersection returns elements common to both slices

func Invert

func Invert[K comparable, V comparable](m map[K]V) map[V]K

Invert swaps keys and values (values must be comparable)

func IsEmpty

func IsEmpty[T any](slice []T) bool

IsEmpty checks if slice is empty

func IsEmptyMap

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

IsEmpty checks if map is empty

func IsNotEmpty

func IsNotEmpty[T any](slice []T) bool

IsNotEmpty checks if slice is not empty

func IsNotEmptyMap

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

IsNotEmpty checks if map is not empty

func Keys

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

Keys returns all keys from the map

func LastIndexOf

func LastIndexOf[T comparable](slice []T, element T) int

LastIndexOf returns the index of the last occurrence of element in slice

func Map

func Map[T, U any](slice []T, transform func(T) U) []U

Map transforms each element of the slice using the provided function

func MapDifference

func MapDifference[K comparable, V any](m1, m2 map[K]V) map[K]V

Difference returns a map containing keys in m1 that are not in m2

func MapIntersection

func MapIntersection[K comparable, V any](m1, m2 map[K]V) map[K]V

Intersection returns a map containing keys common to both maps

func MapKeys

func MapKeys[K comparable, V any, L comparable](m map[K]V, transform func(K) L) map[L]V

MapKeys transforms all keys in the map using the provided function

func MapToInterface

func MapToInterface[K comparable, V any](m map[K]V) map[string]interface{}

MapToInterface converts a map to map[string]interface{}

func MapValues

func MapValues[K comparable, V, U any](m map[K]V, transform func(V) U) map[K]U

MapValues transforms all values in the map using the provided function

func MapsEqual

func MapsEqual[K comparable, V comparable](m1, m2 map[K]V) bool

MapsEqual checks if two maps are equal

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge merges multiple maps into a new map (later maps override earlier ones)

func Omit

func Omit[K comparable, V any](m map[K]V, keys ...K) map[K]V

Omit returns a new map without the specified keys

func Pick

func Pick[K comparable, V any](m map[K]V, keys ...K) map[K]V

Pick returns a new map with only the specified keys

func Reduce

func Reduce[T, U any](slice []T, initial U, reducer func(U, T) U) U

Reduce reduces the slice to a single value using the provided function

func ReduceMap

func ReduceMap[K comparable, V, U any](m map[K]V, initial U, reducer func(U, K, V) U) U

Reduce reduces map to a single value using the provided function

func Remove

func Remove[T comparable](slice []T, element T) []T

Remove removes the first occurrence of element from slice

func RemoveAll

func RemoveAll[T comparable](slice []T, element T) []T

RemoveAll removes all occurrences of element from slice

func RemoveAt

func RemoveAt[T any](slice []T, index int) ([]T, error)

RemoveAt removes element at the specified index

func Reverse

func Reverse[T any](slice []T)

Reverse reverses the slice in place

func Reversed

func Reversed[T any](slice []T) []T

Reversed returns a new reversed slice

func SliceToInterface

func SliceToInterface[T any](slice []T) []interface{}

SliceToInterface converts a typed slice to []interface{}

func SlicesEqual

func SlicesEqual[T comparable](slice1, slice2 []T) bool

SlicesEqual checks if two slices are equal

func Union

func Union[T comparable](slice1, slice2 []T) []T

Union returns all unique elements from both slices

func Unique

func Unique[T comparable](slice []T) []T

Unique removes duplicate elements from slice

func Values

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

Values returns all values from the map

Types

type KeyValue

type KeyValue[K comparable, V any] struct {
	Key   K
	Value V
}

KeyValue represents a key-value pair

func ToSlice

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

ToSlice converts map to slice of key-value pairs

Jump to

Keyboard shortcuts

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