Documentation ¶
Index ¶
- func ContextWithValueSingleton[T any](ctx context.Context, value *T) context.Context
- func FilterSlice[T any](in []T, fn func(T) bool) []T
- func FromContext[T any](ctx context.Context, key any) *T
- func FromContextSingleton[T any](ctx context.Context) *T
- func LimitSlice[T any](slice []T, limit int) ([]T, int)
- func Pointer[T any](v T) *T
- func Value[T any](p *T) T
- type Set
- func (s *Set[T]) Add(value T)
- func (s *Set[T]) AddSet(other *Set[T])
- func (s *Set[T]) Contains(value T) bool
- func (s *Set[T]) Len() int
- func (s Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Remove(value T)
- func (s *Set[T]) Subtract(other *Set[T])
- func (s *Set[T]) ToList() []T
- func (s *Set[T]) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FilterSlice ¶
func FromContextSingleton ¶
func LimitSlice ¶
func Pointer ¶
func Pointer[T any](v T) *T
Pointer creates a pointer from the given value. This is useful, when creating a pointer from a loop variable 1 or when creating a pointer from a return value of another function, since both would require assigning the value to an intermediate variable first.
Types ¶
type Set ¶
type Set[T constraints.Ordered] struct { // contains filtered or unexported fields }
Set implements a set data structure based on built-in maps. This is not optimized for large data, but to be convient.
func NewSet ¶
func NewSet[T constraints.Ordered](values ...T) *Set[T]
NewSet initializes a new Set with the given values. If there are no values provides this is equivalent to new(Set[T]).
func SetIntersect ¶
func SetIntersect[T constraints.Ordered](sets ...*Set[T]) *Set[T]
SetIntersect returns a set that only contains elements which exist in all sets.
func (*Set[T]) Add ¶
func (s *Set[T]) Add(value T)
Add puts a single value to the set. The set will be the same, if it already contains the value.
func (Set[T]) MarshalJSON ¶
MarshalJSON adds support for mashaling the set into a JSON list.
func (*Set[T]) Remove ¶
func (s *Set[T]) Remove(value T)
Remove removes the given value from the set. The set will be the same, if the value is not part of it.
func (*Set[T]) ToList ¶
func (s *Set[T]) ToList() []T
ToList converts the set into a slice. Since the set uses a map as an underlying data structure, this will copy each value. So it might be memory intensive. Also it sorts the slice to ensure a cosistent result.
func (*Set[T]) UnmarshalJSON ¶
MarshalJSON adds support for unmashaling the set from a JSON list.