lnutils

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 4 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 ByteSlice

func ByteSlice[T ByteArray](v T) []byte

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

func ErrorAs[Target error](err error) bool

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.

func Ptr

func Ptr[T any](v T) *T

Ptr returns the pointer of the given value. This is useful in instances where a function returns the value, but a pointer is wanted. Without this, then an intermediate variable is needed.

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 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

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