Documentation
¶
Index ¶
- Variables
- func CastPtr[S any, T any](s *S, castFunc func(S) T) *T
- func Cleanup(ctx context.Context, msg string, fn func() error)
- func CleanupCtx(ctx context.Context, msg string, fn func(ctx context.Context) error)
- func DerefOrDefault[T any](s *T, defaultValue T) T
- func Filter[T any](input []T, f func(T) bool) []T
- func FromPtr[T any](s *T) T
- func IsGTEVersion(curVersion, minVersion string) (bool, error)
- func IsSmallerVersion(curVersion, maxVersionExcluded string) (bool, error)
- func IsVersion(curVersion, eqVersion string) bool
- func Map[T any, U any](input []T, f func(T) U) []U
- func MapValues[K comparable, V any](m map[K]V) []V
- func Must[T any](obj T, err error) T
- func OptionalEnv(key string, envUsageMsg string) (string, bool)
- func RenameOrDeleteFile(ctx context.Context, oldPath, newPath string) error
- func RequiredEnv(key string, envUsageMsg string) string
- func ShallowCopyMap[K comparable, V any](m map[K]V) map[K]V
- func Sprintp[T any](s *T) string
- func SymlinkForce(oldname, newname string) error
- func ToPtr[T any](v T) *T
- func TransformTo[S, T any](iterator iter.Seq[S], f func(S) T) iter.Seq[T]
- func Truncate(s string, maxLen int) string
- type AdjustableSemaphore
- type ErrorCollector
- type ErrorOnce
- type Lazy
- type NotSetError
- type Promise
- type Semaphore
- type SetOnce
- type WaitMap
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadySet = fmt.Errorf("value already set")
Functions ¶
func CleanupCtx ¶
func DerefOrDefault ¶
func DerefOrDefault[T any](s *T, defaultValue T) T
func Filter ¶
Filter takes a slice of any type T and a predicate function f. It returns a new slice containing only the elements from the input slice for which the predicate function returns true.
func IsGTEVersion ¶
func IsSmallerVersion ¶
func Map ¶
Map goes through each item in the input slice and applies the function f to it. It returns a new slice with the results.
func MapValues ¶
func MapValues[K comparable, V any](m map[K]V) []V
MapValues takes a map and returns a slice of all its values.
func OptionalEnv ¶
OptionalEnv returns the value of the environment variable for key if it is set, non-empty and not only whitespace.
Pass the envUsageMsg to describe what the environment variable is used for. This will be used in the message that is printed if the environment variable is not returned.
func RenameOrDeleteFile ¶
RenameOrDeleteFile tries to rename a file but will not replace the target if it already exists. If the file already exists, the file will be deleted. The old file will always be deleted.
func RequiredEnv ¶
RequiredEnv returns the value of the environment variable for key if it is set, non-empty and not only whitespace. It panics otherwise.
Pass the envUsageMsg to describe what the environment variable is used for. This will be used in the error message.
func ShallowCopyMap ¶
func ShallowCopyMap[K comparable, V any](m map[K]V) map[K]V
ShallowCopyMap creates a new map with the same keys and values as the input map. Warning: Do not use this function to copy maps that contain maps or slices.
func SymlinkForce ¶
Types ¶
type AdjustableSemaphore ¶
type AdjustableSemaphore struct {
// contains filtered or unexported fields
}
func NewAdjustableSemaphore ¶
func NewAdjustableSemaphore(limit int64) (*AdjustableSemaphore, error)
func (*AdjustableSemaphore) Acquire ¶
func (s *AdjustableSemaphore) Acquire(ctx context.Context, n int64) error
func (*AdjustableSemaphore) Release ¶
func (s *AdjustableSemaphore) Release(n int64)
func (*AdjustableSemaphore) SetLimit ¶
func (s *AdjustableSemaphore) SetLimit(limit int64) error
func (*AdjustableSemaphore) TryAcquire ¶
func (s *AdjustableSemaphore) TryAcquire(n int64) bool
type ErrorCollector ¶
type ErrorCollector struct {
// contains filtered or unexported fields
}
ErrorCollector collects errors from multiple goroutines. This has a similar API to `errgroup.Group`, except that it runs all goroutines, and returns all errors; it does not stop after the first error.
func NewErrorCollector ¶
func NewErrorCollector(maxConcurrency int) *ErrorCollector
NewErrorCollector creates a new ErrorCollector
func (*ErrorCollector) Wait ¶
func (ec *ErrorCollector) Wait() error
type ErrorOnce ¶
type ErrorOnce struct {
// contains filtered or unexported fields
}
ErrorOnce is a wrapper around SetOnce that can only be set with an error. It's useful for cases where you only need to signal completion with a potential error, without carrying any value.
func (*ErrorOnce) Done ¶
func (e *ErrorOnce) Done() <-chan struct{}
Done returns a channel that's closed when an error is set. This allows using ErrorOnce in select statements.
func (*ErrorOnce) Error ¶
Error returns the error if one has been set, or ErrNotSet if not set yet. Unlike Wait, this doesn't block.
func (*ErrorOnce) SetError ¶
SetError sets the error once. Subsequent calls will return ErrAlreadySet.
func (*ErrorOnce) SetSuccess ¶
SetSuccess marks the operation as completed successfully (no error). This is equivalent to SetError(nil).
type Lazy ¶
type Lazy[T any] struct { // contains filtered or unexported fields }
Lazy provides lazy initialization with memoization. Similar to sync.OnceValue, but accepts the function at call time rather than initialization, allowing the caller to pass context or other parameters.
GetOrInit is safe for concurrent use. Only the first call executes f, and all callers receive the same result. Concurrent callers block until f completes.
Implemented as described here: https://goperf.dev/01-common-patterns/lazy-init/
type NotSetError ¶
type NotSetError struct{}
func (NotSetError) Error ¶
func (e NotSetError) Error() string
type Promise ¶
type Promise[T any] struct { // contains filtered or unexported fields }
Promise represents an asynchronous computation that will eventually produce a value or error. The computation starts immediately when the promise is created. Multiple goroutines can safely wait on the same promise.
func NewPromise ¶
NewPromise creates a new Promise that immediately starts executing the given function in a goroutine. The result (value or error) will be available via Wait.
func (*Promise[T]) Done ¶
func (p *Promise[T]) Done() <-chan struct{}
Done returns a channel that's closed when the promise is resolved. This allows using Promise in select statements.
func (*Promise[T]) Result ¶
Result returns the current result without blocking. Returns NotSetError if the promise hasn't resolved yet.
type SetOnce ¶
type SetOnce[T any] struct { // Don't close the channel from outside, it's used to signal that the value is set. Done chan struct{} // contains filtered or unexported fields }
func NewSetOnce ¶
func (*SetOnce[T]) Result ¶
Result returns the value or error set by SetValue or SetError. It can be called multiple times, returning the same value or error. If called before the value is set, it will return the zero value and NotSetError error.
type WaitMap ¶
type WaitMap struct {
// contains filtered or unexported fields
}
WaitMap allows you to wait for functions with given keys and execute them only once.
func NewWaitMap ¶
func NewWaitMap() *WaitMap