common

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package common holds generic functionality that is common to multiple packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Keys added in v0.6.5

func Keys[K comparable, V any](m map[K]V) []K

Keys returns the keys of a map in the form of a slice.

func Panicf added in v0.9.0

func Panicf(format string, args ...any)

Panicf panics with an error constructed with the given format and args.

func Pause added in v0.9.0

func Pause()

Pause the current goroutine, it waits forever on a channel. Used for testing.

func ReplaceEnvVars added in v0.10.0

func ReplaceEnvVars(str string) string

ReplaceEnvVars makes bash-like substitutions of environment variables, when preceded by '$'.

Example: if `HOME=/home/user` and `F=abc`, then `SubstituteEnvVars("${HOME}/$F/def")` returns the string `"/home/user/abc/def"`.

func ReplaceTildeInDir added in v0.7.1

func ReplaceTildeInDir(dir string) string

ReplaceTildeInDir by the user's home directory. Returns dir if it doesn't start with "~".

It may panic with an error if `dir` has an unknown user (e.g: `~unknown/...`)

func SendNoBlock added in v0.9.0

func SendNoBlock[T any](c chan T, value T) (status int)

SendNoBlock tries to send value through the channel. It returns 0 if the value was sent, 1 if sending it would block (channel buffer full) or 2 if the channel `c` was closed.

func SortedKeys

func SortedKeys[K constraints.Ordered, V any](m map[K]V) []K

SortedKeys returns the sorted keys of a map in the form of a slice.

func TrySend added in v0.9.0

func TrySend[T any](c chan T, value T) (ok bool)

TrySend tries to send value through the channel. It returns false if it failed, presumably because the channel is closed.

func UniqueId added in v0.9.0

func UniqueId() string

UniqueId returns newly created unique id.

func WalkDirWithSymbolicLinks(root string, dirFunc fs.WalkDirFunc) error

WalkDirWithSymbolicLinks is similar filepath.WalkDir, but it follows symbolic links. It also checks for infinite loops in symbolic links, and returns an error if it finds one.

Types

type ArrayFlag added in v0.9.0

type ArrayFlag []string

ArrayFlag implements a flag type that append repeated settings into an array (slice). TODO: make it generic and accept `float64` and `int`.

func (*ArrayFlag) Set added in v0.9.0

func (f *ArrayFlag) Set(value string) error

Set new value, by appending to the end of the string.

func (*ArrayFlag) String added in v0.9.0

func (f *ArrayFlag) String() string

String representation.

type Latch added in v0.9.0

type Latch struct {
	// contains filtered or unexported fields
}

Latch implements a "latch" synchronization mechanism.

A Latch is a signal that can be waited for until it is triggered. Once triggered it never changes state, it's forever triggered.

func NewLatch added in v0.9.0

func NewLatch() *Latch

NewLatch returns an un-triggered latch.

func (*Latch) Test added in v0.9.0

func (l *Latch) Test() bool

Test checks whether the latch has been triggered.

func (*Latch) Trigger added in v0.9.0

func (l *Latch) Trigger()

Trigger latch.

func (*Latch) Wait added in v0.9.0

func (l *Latch) Wait()

Wait waits for the latch to be triggered.

func (*Latch) WaitChan added in v0.9.0

func (l *Latch) WaitChan() <-chan struct{}

WaitChan returns the channel that one can use on a `select` to check when the latch triggers. The returned channel is closed when the latch is triggered.

type LatchWithValue added in v0.9.0

type LatchWithValue[T any] struct {
	// contains filtered or unexported fields
}

LatchWithValue implements a "latch" synchronization mechanism, with a value associated with the triggering of the latch.

A LatchWithValue is a signal that can be waited for until it is triggered. Once triggered it never changes state, it's forever triggered.

func NewLatchWithValue added in v0.9.0

func NewLatchWithValue[T any]() *LatchWithValue[T]

NewLatchWithValue returns an un-triggered latch.

func (*LatchWithValue[T]) Test added in v0.9.0

func (l *LatchWithValue[T]) Test() bool

Test checks whether the latch has been triggered.

func (*LatchWithValue[T]) Trigger added in v0.9.0

func (l *LatchWithValue[T]) Trigger(value T)

Trigger latch and saves the associated value.

func (*LatchWithValue[T]) Wait added in v0.9.0

func (l *LatchWithValue[T]) Wait() T

Wait waits for the latch to be triggered.

type Set

type Set[T comparable] map[T]struct{}

Set implements a Set for the key type T.

func MakeSet

func MakeSet[T comparable](size ...int) Set[T]

MakeSet returns an empty Set of the given type. Size is optional, and if given will reserve the expected size.

func SetWithValues added in v0.10.0

func SetWithValues[T comparable](values ...T) Set[T]

SetWithValues creates a new Set initialized with the given `values`.

func (Set[T]) Delete added in v0.6.5

func (s Set[T]) Delete(key T)

Delete key into set.

func (Set[T]) Has

func (s Set[T]) Has(key T) bool

Has returns true if Set s has the given key.

func (Set[T]) Insert

func (s Set[T]) Insert(key T)

Insert key into set.

Jump to

Keyboard shortcuts

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