Documentation ¶
Index ¶
- func ByteSlice[T ByteArray](v T) []byte
- func ErrorAs[Target error](err error) bool
- func Map[T1, T2 any](s []T1, f func(T1) T2) []T2
- func Ptr[T any](v T) *T
- func RecvOrTimeout[T any](c <-chan T, timeout time.Duration) (*T, error)
- type ByteArray
- type SyncMap
- func (m *SyncMap[K, V]) Delete(key K)
- func (m *SyncMap[K, V]) ForEach(visitor func(K, V) error)
- func (m *SyncMap[K, V]) Len() int
- func (m *SyncMap[K, V]) Load(key K) (V, bool)
- func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool)
- func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool)
- func (m *SyncMap[K, V]) Range(visitor func(K, V) bool)
- func (m *SyncMap[K, V]) Store(key K, value V)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ByteSlice ¶
ByteSlice takes a byte array, and returns a slice. This is useful when a function returns an array, but a slice is wanted. Without this, then an intermediate variable is needed.
func ErrorAs ¶
ErrorAs behaves the same as `errors.As` except there's no need to declare the target error as a variable first. Instead of writing:
var targetErr *TargetErr errors.As(err, &targetErr)
We can write:
lnutils.ErrorAs[*TargetErr](err)
To save us from declaring the target error variable.
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.
Types ¶
type ByteArray ¶
type ByteArray interface { ~[32]byte }
ByteArray is a type constraint for type that reduces down to a fixed sized array.
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 ¶
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]) Load ¶
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 ¶
LoadAndDelete queries an item and deletes it from the map using the specified key.
func (*SyncMap[K, V]) LoadOrStore ¶
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.