lnutils

package
v0.16.3-beta Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 3 Imported by: 0

README

lnutils

This package provides fundamental types and utility functions that can be used by all other packages.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map[T1, T2 any](s []T1, f func(T1) T2) []T2

Map takes an input slice, and applies the function f to each element, yielding a new slice.

func RecvOrTimeout

func RecvOrTimeout[T any](c <-chan T, timeout time.Duration) (*T, error)

RecvOrTimeout attempts to recv over chan c, returning the value. If the timeout passes before the recv succeeds, an error is returned.

Types

type SyncMap

type SyncMap[K comparable, V any] struct {
	sync.Map
}

SyncMap wraps a sync.Map with type parameters such that it's easier to access the items stored in the map since no type assertion is needed. It also requires explicit type definition when declaring and initiating the variables, which helps us understanding what's stored in a given map.

func (*SyncMap[K, V]) Delete

func (m *SyncMap[K, V]) Delete(key K)

Delete removes an item from the map specified by the key.

func (*SyncMap[K, V]) ForEach

func (m *SyncMap[K, V]) ForEach(visitor func(K, V) error)

ForEach iterates the map and applies the `visitor` function. Unlike the `Range` method, the `visitor` function will be applied to all the items unless there's an error.

func (*SyncMap[K, V]) Len

func (m *SyncMap[K, V]) Len() int

Len returns the number of items in the map.

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (V, bool)

Load queries an item from the map using the specified key. If the item cannot be found, an empty value and false will be returned. If the stored item fails the type assertion, a nil value and false will be returned.

func (*SyncMap[K, V]) LoadAndDelete

func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool)

LoadAndDelete queries an item and deletes it from the map using the specified key.

func (*SyncMap[K, V]) LoadOrStore

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool)

LoadOrStore queries an item from the map using the specified key. If the item cannot be found, the `value` will be stored in the map and returned. If the stored item fails the type assertion, a nil value and false will be returned.

func (*SyncMap[K, V]) Range

func (m *SyncMap[K, V]) Range(visitor func(K, V) bool)

Range iterates the map and applies the `visitor` function. If the `visitor` returns false, the iteration will be stopped.

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V)

Store puts an item in the map.

Jump to

Keyboard shortcuts

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