Documentation
¶
Overview ¶
Package core provides fundamental helpers for darvaza.org projects
Index ¶
- Constants
- Variables
- func AddrFromNetIP(addr net.Addr) (netip.Addr, bool)
- func AddrPort(v any) (netip.AddrPort, bool)
- func As[T, V any](v T) (V, bool)
- func AsError[T any](v T) error
- func AsErrors[T any](vv []T) []error
- func AsFn[T, V any](fn func(T) (V, bool), v T) (V, bool)
- func AssertContains(t T, s, substr, name string, args ...any) bool
- func AssertEqual[U comparable](t T, expected, actual U, name string, args ...any) bool
- func AssertError(t T, err error, name string, args ...any) bool
- func AssertErrorIs(t T, err, target error, name string, args ...any) bool
- func AssertFalse(t T, value bool, name string, args ...any) bool
- func AssertMustContains(t T, s, substr, name string, args ...any)
- func AssertMustEqual[U comparable](t T, expected, actual U, name string, args ...any)
- func AssertMustError(t T, err error, name string, args ...any)
- func AssertMustErrorIs(t T, err, target error, name string, args ...any)
- func AssertMustFalse(t T, value bool, name string, args ...any)
- func AssertMustNil(t T, value any, name string, args ...any)
- func AssertMustNoError(t T, err error, name string, args ...any)
- func AssertMustNoPanic(t T, fn func(), name string, args ...any)
- func AssertMustNotContain(t T, s, substr, name string, args ...any)
- func AssertMustNotEqual[U comparable](t T, expected, actual U, name string, args ...any)
- func AssertMustNotNil(t T, value any, name string, args ...any)
- func AssertMustNotSame(t T, expected, actual any, name string, args ...any)
- func AssertMustPanic(t T, fn func(), expectedPanic any, name string, args ...any)
- func AssertMustSame(t T, expected, actual any, name string, args ...any)
- func AssertMustSliceEqual[U any](t T, expected, actual []U, name string, args ...any)
- func AssertMustTrue(t T, value bool, name string, args ...any)
- func AssertMustTypeIs[U any](t T, value any, name string, args ...any) U
- func AssertNil(t T, value any, name string, args ...any) bool
- func AssertNoError(t T, err error, name string, args ...any) bool
- func AssertNoPanic(t T, fn func(), name string, args ...any) (ok bool)
- func AssertNotContain(t T, s, substr, name string, args ...any) bool
- func AssertNotEqual[U comparable](t T, expected, actual U, name string, args ...any) bool
- func AssertNotNil(t T, value any, name string, args ...any) bool
- func AssertNotSame(t T, expected, actual any, name string, args ...any) bool
- func AssertPanic(t T, fn func(), expectedPanic any, name string, args ...any) (ok bool)
- func AssertSame(t T, expected, actual any, name string, args ...any) bool
- func AssertSliceEqual[U any](t T, expected, actual []U, name string, args ...any) bool
- func AssertTrue(t T, value bool, name string, args ...any) bool
- func AssertTypeIs[U any](t T, value any, name string, args ...any) (U, bool)
- func Catch(fn func() error) error
- func CheckIsTemporary(err error) (is, known bool)
- func CheckIsTimeout(err error) (is, known bool)
- func Coalesce[T any](opts ...T) T
- func CoalesceError(errs ...error) error
- func GetIPAddresses(ifaces ...string) ([]netip.Addr, error)
- func GetInterfacesNames(except ...string) ([]string, error)
- func GetNetIPAddresses(ifaces ...string) ([]net.IP, error)
- func GetStringIPAddresses(ifaces ...string) ([]string, error)
- func IIf[T any](cond bool, yes, no T) T
- func IsError(err error, errs ...error) bool
- func IsErrorFn(check func(error) bool, errs ...error) bool
- func IsErrorFn2(check func(error) (bool, bool), errs ...error) (is, known bool)
- func IsNil(vi any) bool
- func IsSame(a, b any) bool
- func IsTemporary(err error) bool
- func IsTimeout(err error) bool
- func IsZero(vi any) bool
- func JoinHostPort(host, port string) (string, error)
- func Keys[K comparable, T any](m map[K]T) []K
- func ListContains[T comparable](l *list.List, val T) bool
- func ListContainsFn[T any](l *list.List, val T, eq func(T, T) bool) bool
- func ListCopy[T any](src *list.List) *list.List
- func ListCopyFn[T any](src *list.List, fn func(v T) (T, bool)) *list.List
- func ListForEach[T any](l *list.List, fn func(v T) bool)
- func ListForEachBackward[T any](l *list.List, fn func(v T) bool)
- func ListForEachBackwardElement(l *list.List, fn func(*list.Element) bool)
- func ListForEachElement(l *list.List, fn func(*list.Element) bool)
- func MakeHostPort(hostPort string, defaultPort uint16) (string, error)
- func MapAllListContains[K comparable, T comparable](m map[K]*list.List, v T) bool
- func MapAllListContainsFn[K comparable, T any](m map[K]*list.List, match func(v T) bool) bool
- func MapAllListForEach[K comparable, T any](m map[K]*list.List, fn func(v T) bool)
- func MapAllListForEachElement[K comparable](m map[K]*list.List, fn func(*list.Element) bool)
- func MapContains[K comparable](m map[K]any, key K) bool
- func MapListAppend[K comparable, T any](m map[K]*list.List, key K, v T)
- func MapListAppendUnique[K comparable, T comparable](m map[K]*list.List, key K, v T)
- func MapListAppendUniqueFn[K comparable, T any](m map[K]*list.List, key K, v T, eq func(T, T) bool)
- func MapListContains[K comparable, T comparable](m map[K]*list.List, key K, v T) bool
- func MapListContainsFn[K comparable, T any](m map[K]*list.List, key K, v T, eq func(T, T) bool) bool
- func MapListCopy[T comparable](src map[T]*list.List) map[T]*list.List
- func MapListCopyFn[K comparable, V any](src map[K]*list.List, fn func(v V) (V, bool)) map[K]*list.List
- func MapListForEach[K comparable, T any](m map[K]*list.List, key K, fn func(v T) bool)
- func MapListForEachElement[K comparable](m map[K]*list.List, key K, fn func(el *list.Element) bool)
- func MapListInsert[K comparable, T any](m map[K]*list.List, key K, v T)
- func MapListInsertUnique[K comparable, T comparable](m map[K]*list.List, key K, v T)
- func MapListInsertUniqueFn[K comparable, T any](m map[K]*list.List, key K, v T, eq func(va, vb T) bool)
- func MapValue[K comparable, V any](m map[K]V, key K, def V) (V, bool)
- func Maybe[V any](value V, _ error) V
- func MaybeOK[V any](value V, _ bool) V
- func MaybeT[T any](value any) T
- func Must[V any](value V, err error) V
- func MustOK[V any](value V, ok bool) V
- func MustT[T any](value any) T
- func NewTemporaryError(err error) error
- func NewTimeoutError(err error) error
- func NewUnreachableError(skip int, err error, note string) error
- func NewUnreachableErrorf(skip int, err error, format string, args ...any) error
- func Panic(payload any)
- func PanicWrap(err error, note string)
- func PanicWrapf(err error, format string, args ...any)
- func Panicf(format string, args ...any)
- func ParseAddr(s string) (addr netip.Addr, err error)
- func ParseNetIP(s string) (ip net.IP, err error)
- func QuietWrap(err error, format string, args ...any) error
- func RunBenchmark(b *testing.B, setup func() any, fn func(any))
- func RunConcurrentTest(t T, numWorkers int, worker func(int) error) error
- func RunTestCases[T TestCase](t *testing.T, cases []T)
- func S[T any](v ...T) []T
- func SliceAs[T, V any](vv []T) []V
- func SliceAsFn[T, V any](fn func(T) (V, bool), vv []T) []V
- func SliceContains[T comparable](a []T, v T) bool
- func SliceContainsFn[T any](a []T, v T, eq func(T, T) bool) bool
- func SliceCopy[T any](s []T) []T
- func SliceCopyFn[T any](s []T, fn func(partial []T, before T) (after T, include bool)) []T
- func SliceEqual[T comparable](a, b []T) bool
- func SliceEqualFn[T any](a, b []T, eq func(va, vb T) bool) bool
- func SliceMap[T1 any, T2 any](a []T1, fn func(partial []T2, v T1) (newEntries []T2)) []T2
- func SliceMinus[T comparable](a, b []T) []T
- func SliceMinusFn[T any](a, b []T, eq func(T, T) bool) []T
- func SliceP[T Ordered](a []T, p float64) T
- func SliceRandom[T any](a []T) (T, bool)
- func SliceReplaceFn[T any](s []T, fn func(partial []T, before T) (after T, replace bool)) []T
- func SliceReverse[T any](x []T)
- func SliceReversed[T any](a []T) []T
- func SliceReversedFn[T any](a []T, fn func(partial []T, before T) (after T, include bool)) []T
- func SliceSort[T any](x []T, cmp func(a, b T) int)
- func SliceSortFn[T any](x []T, less func(a, b T) bool)
- func SliceSortOrdered[T Ordered](x []T)
- func SliceUnique[T comparable](a []T) []T
- func SliceUniqueFn[T any](a []T, eq func(T, T) bool) []T
- func SliceUniquify[T comparable](ptr *[]T) []T
- func SliceUniquifyFn[T any](ptr *[]T, eq func(T, T) bool) []T
- func SortedKeys[K Ordered, T any](m map[K]T) []K
- func SortedValues[K Ordered, T any](m map[K]T) []T
- func SortedValuesCond[K Ordered, T any](m map[K]T, fn func(T) bool) []T
- func SortedValuesUnlikelyCond[K Ordered, T any](m map[K]T, fn func(T) bool) []T
- func SplitAddrPort(addrPort string) (addr netip.Addr, port uint16, err error)
- func SplitHostPort(hostPort string) (host, port string, err error)
- func Unwrap(err error) []error
- func WithTimeout(parent context.Context, tio time.Duration) (context.Context, context.CancelFunc)
- func WithTimeoutCause(parent context.Context, tio time.Duration, cause error) (context.Context, context.CancelFunc)
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...any) error
- func Zero[T any](_ *T) T
- type Bool
- type CallStacker
- type Catcher
- type Complex
- type CompoundError
- func (w *CompoundError) Append(err error, note string, args ...any) *CompoundError
- func (w *CompoundError) AppendError(errs ...error) *CompoundError
- func (w *CompoundError) AsError() error
- func (w *CompoundError) Error() string
- func (w *CompoundError) Errors() []error
- func (w *CompoundError) OK() bool
- func (w *CompoundError) Ok() booldeprecated
- func (w *CompoundError) Unwrap() []error
- type ContextKey
- type ErrGroup
- func (eg *ErrGroup) Cancel(cause error) bool
- func (eg *ErrGroup) Cancelled() <-chan struct{}
- func (eg *ErrGroup) Context() context.Context
- func (eg *ErrGroup) Done() <-chan struct{}
- func (eg *ErrGroup) Err() error
- func (eg *ErrGroup) Go(run func(context.Context) error, shutdown func() error)
- func (eg *ErrGroup) GoCatch(run func(context.Context) error, catch func(context.Context, error) error)
- func (eg *ErrGroup) IsCancelled() bool
- func (eg *ErrGroup) OnError(fn func(error))
- func (eg *ErrGroup) SetDefaults()
- func (eg *ErrGroup) Wait() error
- type Errors
- type Float
- type Frame
- func (f Frame) File() string
- func (f Frame) FileLine() string
- func (f Frame) Format(s fmt.State, verb rune)
- func (f Frame) FuncName() string
- func (f Frame) Line() int
- func (f Frame) Name() string
- func (f Frame) PkgFile() string
- func (f Frame) PkgName() string
- func (f Frame) SplitName() (pkgName, funcName string)
- func (f Frame) String() string
- type Integer
- type MockT
- func (m *MockT) Error(args ...any)
- func (m *MockT) Errorf(format string, args ...any)
- func (m *MockT) Fail()
- func (m *MockT) FailNow()
- func (m *MockT) Failed() bool
- func (m *MockT) Fatal(args ...any)
- func (m *MockT) Fatalf(format string, args ...any)
- func (m *MockT) HasErrors() bool
- func (m *MockT) HasLogs() bool
- func (m *MockT) Helper()
- func (m *MockT) LastError() (string, bool)
- func (m *MockT) LastLog() (string, bool)
- func (m *MockT) Log(args ...any)
- func (m *MockT) Logf(format string, args ...any)
- func (m *MockT) Reset()
- func (m *MockT) Run(_ string, f func(T)) (ok bool)
- type Ordered
- type PanicError
- type Recovered
- type Signed
- type SpinLockdeprecated
- type Stack
- type String
- type T
- type TemporaryError
- type TestCase
- type Unsigned
- type Unwrappable
- type WaitGroup
- type WrappedError
Constants ¶
const (
// MaxDepth is the maximum depth we will go in the stack.
MaxDepth = 32
)
Variables ¶
var ( // ErrNotImplemented indicates something hasn't been implemented yet ErrNotImplemented = errors.New("not implemented") // ErrTODO is like ErrNotImplemented but used especially to // indicate something needs to be implemented ErrTODO = Wrap(ErrNotImplemented, "TODO") // ErrExists indicates something already exists ErrExists = errors.New("already exists") // ErrNotExists indicates something doesn't exist ErrNotExists = errors.New("does not exist") // ErrInvalid indicates an argument isn't valid ErrInvalid = errors.New("invalid argument") // ErrUnknown indicates something isn't recognized ErrUnknown = errors.New("unknown") // ErrNilReceiver indicates a method was called over a nil instance ErrNilReceiver = errors.New("nil receiver") // ErrUnreachable indicates something impossible happened ErrUnreachable = errors.New("unreachable") )
Functions ¶
func AddrFromNetIP ¶
AddrFromNetIP attempts to convert a net.Addr into a netip.Addr
func AddrPort ¶
AddrPort attempts to extract a netip.AddrPort from an object. It supports the following types:
- netip.AddrPort (returned as-is)
- *netip.AddrPort (dereferenced)
- *net.TCPAddr (converted with IPv4 unmapping)
- *net.UDPAddr (converted with IPv4 unmapping)
- Types implementing AddrPort() netip.AddrPort method (if result is valid)
- Types implementing Addr() net.Addr method (recursively processed)
- Types implementing RemoteAddr() net.Addr method (recursively processed)
IPv4 addresses are properly unmapped, so 192.168.1.1:80 is returned instead of [::ffff:192.168.1.1]:80. Invalid AddrPort values return false.
func AsError ¶ added in v0.14.8
AsError attempts to convert a value to an error, checking different interfaces.
* AsError() error * Error() string * OK() bool * IsZero() bool
func AsErrors ¶ added in v0.14.8
AsErrors uses AsError to return the subset of the elements that are errors.
func AssertContains ¶ added in v0.18.0
AssertContains fails the test if the string does not contain the substring. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertContains(t, "hello world", "world", "substring check") AssertContains(t, output, "success", "command output for %s", cmd)
func AssertEqual ¶ added in v0.18.0
func AssertEqual[U comparable](t T, expected, actual U, name string, args ...any) bool
AssertEqual compares two values and reports differences. This is a generic helper that works with any comparable type. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertEqual(t, 42, result, "result value") AssertEqual(t, "hello", str, "string %d comparison", 1)
func AssertError ¶ added in v0.18.0
AssertError fails the test if error is nil. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertError(t, err, "parse error") AssertError(t, err, "operation %s", "save")
func AssertErrorIs ¶ added in v0.18.0
AssertErrorIs fails the test if the error does not match the target error. Uses errors.Is to check if the error matches. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertErrorIs(t, err, ErrNotFound, "lookup error") AssertErrorIs(t, err, ErrInvalid, "validation for %s", field)
func AssertFalse ¶ added in v0.18.0
AssertFalse fails the test if value is not false. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertFalse(t, hasError, "no errors expected") AssertFalse(t, isEmpty, "container %s should not be empty", name)
revive:disable-next-line:flag-parameter
func AssertMustContains ¶ added in v0.18.1
AssertMustContains calls AssertContains and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustContains(t, "hello world", "world", "substring check") AssertMustContains(t, output, "success", "command output for %s", cmd)
func AssertMustEqual ¶ added in v0.18.1
func AssertMustEqual[U comparable](t T, expected, actual U, name string, args ...any)
AssertMustEqual calls AssertEqual and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustEqual(t, 42, result, "result value") AssertMustEqual(t, "hello", str, "string %d comparison", 1)
func AssertMustError ¶ added in v0.18.1
AssertMustError calls AssertError and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustError(t, err, "parse error") AssertMustError(t, err, "operation %s", "save")
func AssertMustErrorIs ¶ added in v0.18.1
AssertMustErrorIs calls AssertErrorIs and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustErrorIs(t, err, ErrNotFound, "lookup error") AssertMustErrorIs(t, err, ErrInvalid, "validation for %s", field)
func AssertMustFalse ¶ added in v0.18.1
AssertMustFalse calls AssertFalse and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustFalse(t, hasError, "no errors expected") AssertMustFalse(t, isEmpty, "container %s should not be empty", name)
revive:disable-next-line:flag-parameter
func AssertMustNil ¶ added in v0.18.1
AssertMustNil calls AssertNil and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustNil(t, err, "error should be nil") AssertMustNil(t, ptr, "pointer %s should be nil", ptrName)
func AssertMustNoError ¶ added in v0.18.1
AssertMustNoError calls AssertNoError and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustNoError(t, err, "initialization") AssertMustNoError(t, err, "loading %s", filename)
func AssertMustNoPanic ¶ added in v0.18.1
AssertMustNoPanic calls AssertNoPanic and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustNoPanic(t, func() { safeFunction() }, "safe function")
AssertMustNoPanic(t, func() { handleNilInput(nil) }, "nil input %s", "handling")
func AssertMustNotContain ¶ added in v0.18.4
AssertMustNotContain calls AssertNotContain and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustNotContain(t, "hello world", "goodbye", "substring absence check") AssertMustNotContain(t, output, "error", "no errors in %s", cmd)
func AssertMustNotEqual ¶ added in v0.18.1
func AssertMustNotEqual[U comparable](t T, expected, actual U, name string, args ...any)
AssertMustNotEqual calls AssertNotEqual and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustNotEqual(t, 42, result, "result value") AssertMustNotEqual(t, "hello", str, "string %d comparison", 1)
func AssertMustNotNil ¶ added in v0.18.1
AssertMustNotNil calls AssertNotNil and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustNotNil(t, result, "result should not be nil") AssertMustNotNil(t, m, "map %s should not be nil", mapName)
func AssertMustNotSame ¶ added in v0.18.2
AssertMustNotSame asserts that two values are not the same, calling t.FailNow() on failure.
For value types (numbers, strings, booleans), same-ness means equal values. For reference types (slices, maps, pointers, channels, functions), same-ness means pointer equality to the same underlying data structure.
AssertMustNotSame(t, slice1, slice2, "slice reference") AssertMustNotSame(t, 42, 43, "number value")
func AssertMustPanic ¶ added in v0.18.1
AssertMustPanic calls AssertPanic and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustPanic(t, func() { someFunctionThatPanics() }, nil, "panic test")
AssertMustPanic(t, func() { divide(1, 0) }, "division by zero", "divide %d by zero", 1)
func AssertMustSame ¶ added in v0.18.2
AssertMustSame asserts that two values are the same, calling t.FailNow() on failure.
For value types (numbers, strings, booleans), same-ness means equal values. For reference types (slices, maps, pointers, channels, functions), same-ness means pointer equality to the same underlying data structure.
AssertMustSame(t, slice1, slice2, "slice reference") AssertMustSame(t, 42, 42, "number value")
func AssertMustSliceEqual ¶ added in v0.18.1
AssertMustSliceEqual calls AssertSliceEqual and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustSliceEqual(t, S(1, 2, 3), result, "result slice")
AssertMustSliceEqual(t, S("a", "b"), strings, "string slice %s", "test")
func AssertMustTrue ¶ added in v0.18.1
AssertMustTrue calls AssertTrue and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure.
Example usage:
AssertMustTrue(t, result, "operation succeeded") AssertMustTrue(t, isValid, "validation for %s", field)
revive:disable-next-line:flag-parameter
func AssertMustTypeIs ¶ added in v0.18.1
AssertMustTypeIs calls AssertTypeIs and t.FailNow() if the assertion fails. This is a convenience function for tests that should terminate on assertion failure. Returns the cast value on success, or the zero value if the test fails.
Example usage:
val := AssertMustTypeIs[*MyError](t, err, "error type") config := AssertMustTypeIs[*Config](t, result, "config type for %s", name)
func AssertNil ¶ added in v0.18.0
AssertNil asserts that a value is nil. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertNil(t, err, "error should be nil") AssertNil(t, ptr, "pointer %s should be nil", ptrName)
func AssertNoError ¶ added in v0.18.0
AssertNoError fails the test if error is not nil. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertNoError(t, err, "initialization") AssertNoError(t, err, "loading %s", filename)
func AssertNoPanic ¶ added in v0.18.0
AssertNoPanic runs a function expecting it not to panic. This is useful for testing that functions handle edge cases gracefully. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertNoPanic(t, func() { safeFunction() }, "safe function")
AssertNoPanic(t, func() { handleNilInput(nil) }, "nil input %s", "handling")
func AssertNotContain ¶ added in v0.18.4
AssertNotContain fails the test if the string contains the substring. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertNotContain(t, "hello world", "goodbye", "substring absence check") AssertNotContain(t, output, "error", "command output for %s", cmd)
func AssertNotEqual ¶ added in v0.18.0
func AssertNotEqual[U comparable](t T, expected, actual U, name string, args ...any) bool
AssertNotEqual compares two values and ensures they are different. This is a generic helper that works with any comparable type. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertNotEqual(t, 42, result, "result value") AssertNotEqual(t, "hello", str, "string %d comparison", 1)
func AssertNotNil ¶ added in v0.18.0
AssertNotNil asserts that a value is not nil. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertNotNil(t, result, "result should not be nil") AssertNotNil(t, m, "map %s should not be nil", mapName)
func AssertNotSame ¶ added in v0.18.2
AssertNotSame asserts that two values are not the same. For reference types, this tests that they do not point to the same underlying data. For value types, this tests that they have different values. This is useful for testing that two slices, maps, or pointers reference different memory locations, even if they have equal contents. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
slice1 := []int{1, 2, 3}
slice2 := []int{1, 2, 3} // same content, different backing array
AssertNotSame(t, slice1, slice2, "slice reference")
map1 := make(map[string]int)
map2 := make(map[string]int) // different maps
AssertNotSame(t, map1, map2, "map reference")
AssertNotSame(t, 42, 43, "number value")
AssertNotSame(t, "hello", "world", "string value")
func AssertPanic ¶ added in v0.18.0
AssertPanic runs a function expecting it to panic and optionally validates the panic value. The expectedPanic parameter determines how the panic value is validated:
- nil: Any panic is acceptable (most common case - just verify it panics).
- error: Uses errors.Is semantics to match error chains (resilient to wrapping).
- string: Checks if the panic message contains this substring (resilient to message changes).
- Recovered: Direct comparison without unwrapping (for testing panic recovery).
- other types: Exact equality check after unwrapping Recovered if present.
This type-specific matching makes tests more resilient to implementation changes whilst still validating that panics occur for the right reasons. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
// Just verify it panics (most common)
AssertPanic(t, func() { slice[999] }, nil, "out of bounds")
// Check panic message contains substring
AssertPanic(t, func() { divide(1, 0) }, "division", "divide by zero")
// Check panic with specific error type
AssertPanic(t, func() { mustValidate(nil) }, ErrValidation, "validation")
func AssertSame ¶ added in v0.18.2
AssertSame asserts that two values are the same. For reference types, this tests that they point to the same underlying data. For value types, this tests that they have equal values. This is useful for testing that two slices, maps, or pointers reference the same memory location, not just equal contents. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
slice1 := []int{1, 2, 3}
slice2 := slice1
AssertSame(t, slice1, slice2, "slice reference")
map1 := make(map[string]int)
map2 := map1
AssertSame(t, map1, map2, "map reference")
AssertSame(t, 42, 42, "number value")
AssertSame(t, "hello", "hello", "string value")
func AssertSliceEqual ¶ added in v0.18.0
AssertSliceEqual compares two slices and reports differences. This uses reflect.DeepEqual for comprehensive comparison. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertSliceEqual(t, S(1, 2, 3), result, "result slice")
AssertSliceEqual(t, S("a", "b"), strings, "string slice %s", "test")
func AssertTrue ¶ added in v0.18.0
AssertTrue fails the test if value is not true. The name parameter can include printf-style formatting. Returns true if the assertion passed, false otherwise.
Example usage:
AssertTrue(t, result, "operation succeeded") AssertTrue(t, isValid, "validation for %s", field)
revive:disable-next-line:flag-parameter
func AssertTypeIs ¶ added in v0.18.0
AssertTypeIs fails the test if value is not of the expected type. It returns the value cast to the expected type and a boolean indicating success. The name parameter can include printf-style formatting.
Example usage:
val, ok := AssertTypeIs[*MyError](t, err, "error type") config, ok := AssertTypeIs[*Config](t, result, "config type for %s", name)
func Catch ¶ added in v0.14.10
Catch uses a Catcher to safely call a function and return the organic error or the Recovered PanicError.
func CheckIsTemporary ¶ added in v0.13.2
CheckIsTemporary tests an error for temporary conditions without unwrapping. It checks if the error implements Temporary() bool or IsTemporary() bool interfaces directly, without traversing wrapped error chains.
The function examines the error in the following priority order:
- Temporary() bool interface (legacy net.Error style)
- IsTemporary() bool interface (modern style)
- Falls back to CheckIsTimeout for timeout-based temporary errors
Returns:
- is: true if the error indicates a temporary condition
- known: true if the error type implements a recognized interface
For nil errors, returns (false, true) indicating definitively not temporary. For errors with no recognized interface, returns result from CheckIsTimeout.
func CheckIsTimeout ¶ added in v0.13.2
CheckIsTimeout tests an error for timeout conditions without unwrapping. It checks if the error implements Timeout() bool or IsTimeout() bool interfaces directly, without traversing wrapped error chains.
The function examines the error in the following priority order:
- Timeout() bool interface (legacy net.Error style)
- IsTimeout() bool interface (modern style)
Returns:
- is: true if the error indicates a timeout condition
- known: true if the error type implements a recognized timeout interface
For nil errors, returns (false, true) indicating definitively not a timeout. For errors with no recognized timeout interface, returns (false, false).
Note that timeout errors are typically also considered temporary conditions, but this function specifically tests for timeout semantics only.
func CoalesceError ¶ added in v0.9.5
CoalesceError returns the first non-nil error argument. error isn't compatible with Coalesce's comparable generic type.
func GetIPAddresses ¶
GetIPAddresses returns a list of netip.Addr bound to the given interfaces or all if none are given
func GetInterfacesNames ¶
GetInterfacesNames returns the list of interfaces, considering an optional exclusion list
func GetNetIPAddresses ¶
GetNetIPAddresses returns a list of net.IP addresses bound to the given interfaces or all if none are given
func GetStringIPAddresses ¶
GetStringIPAddresses returns a list of text IP addresses bound to the given interfaces or all if none are given
func IsError ¶ added in v0.13.0
IsError recursively check if the given error is in in the given list, or just non-nil if no options to check are given.
func IsErrorFn ¶ added in v0.13.0
IsErrorFn recursively checks if any of the given errors satisfies the specified check function.
revive:disable:cognitive-complexity
func IsErrorFn2 ¶ added in v0.13.0
IsErrorFn2 recursively checks if any of the given errors gets a certain answer from the check function. As opposed to IsErrorFn, IsErrorFn2 will stop when it has certainty of a false result.
revive:disable:cognitive-complexity
func IsNil ¶ added in v0.17.5
IsNil reports whether vi is nil or a typed nil value. It answers the question: "Is this value nil (typed or untyped)?"
IsNil returns true for:
- nil (untyped nil)
- nil pointers, slices, maps, channels, functions, and interfaces
- reflect.Value that is invalid or has a nil underlying value
IsNil returns false for:
- Zero values of basic types (0, "", false) - these are not nil
- Non-nil pointers (even if pointing to zero values)
- Initialized empty collections ([]int{}, map[string]int{})
- Non-nil interfaces containing zero values
- Zero-valued structs (structs cannot be nil)
The key distinction from IsZero: IsNil only checks for nil state, while IsZero checks for uninitialized state (which includes nil).
Comparison with IsZero:
- IsNil(nil) // true - untyped nil
- IsNil(0) // false - zero int is not nil
- IsNil("") // false - zero string is not nil
- IsNil([]int(nil)) // true - nil slice
- IsNil([]int{}) // false - empty slice is not nil
- IsNil((*int)(nil)) // true - nil pointer
- IsNil(new(int)) // false - non-nil pointer
- IsNil(struct{}{}) // false - structs cannot be nil
Example:
var ptr *int
IsNil(ptr) // true - nil pointer
IsZero(ptr) // true - nil pointer is also zero
var slice []int
IsNil(slice) // true - nil slice
IsZero(slice) // true - nil slice is also zero
slice = []int{}
IsNil(slice) // false - empty slice is not nil
IsZero(slice) // false - empty slice is initialized
var num int
IsNil(num) // false - integers cannot be nil
IsZero(num) // true - zero integer is uninitialized
func IsSame ¶ added in v0.18.2
IsSame reports whether two values are the same. It answers the question: "Are these values the same?"
For reference types, IsSame returns true when they point to the same underlying data:
- Two slices pointing to the same backing array.
- Two maps pointing to the same map data.
- Two channels pointing to the same channel.
- Two function values pointing to the same function.
- Two pointers pointing to the same address.
- Two interfaces containing the same pointer value.
For value types, IsSame returns true when they have equal values:
- Numbers with the same value (42 == 42).
- Strings with the same content ("hello" == "hello").
- Booleans with the same value (true == true).
IsSame returns false for:
- Different backing arrays/maps/channels/functions/pointers.
- Different values for basic types.
- One nil and one non-nil value.
- Different types.
- Arrays, structs, and other composite types (not handled).
Special case for slices: Go's runtime optimises zero-capacity slices (make([]T, 0)) to share a common zero-sized allocation. IsSame treats these as distinct to preserve expected semantics.
Example:
// Value types - compared by value
IsSame(42, 42) // true - same value
IsSame(42, 43) // false - different values
IsSame("hello", "hello") // true - same string content
IsSame("hello", "world") // false - different strings
// Nil handling
IsSame(nil, nil) // true - both untyped nil
var s1, s2 []int // both nil slices of same type
IsSame(s1, s2) // true - both nil of same type
IsSame(s1, []int{}) // false - one nil, one empty slice
// Reference types - compared by reference
slice1 := []int{1, 2, 3}
slice2 := slice1 // same backing array
slice3 := []int{1, 2, 3} // different backing array
IsSame(slice1, slice2) // true - same backing array
IsSame(slice1, slice3) // false - different backing arrays
map1 := make(map[string]int)
map2 := map1 // same map
map3 := make(map[string]int) // different map
IsSame(map1, map2) // true - same map
IsSame(map1, map3) // false - different maps
x := 42
ptr1 := &x
ptr2 := ptr1 // same pointer
ptr3 := &x // different pointer variable, same address
IsSame(ptr1, ptr2) // true - same pointer
IsSame(ptr1, ptr3) // true - both point to same address
func IsTemporary ¶ added in v0.13.2
IsTemporary tests an error chain for temporary conditions recursively. It traverses wrapped error chains using IsErrorFn2 to find any error that implements temporary condition interfaces.
This function provides comprehensive temporary error detection by:
- Checking each error in the unwrapping chain via CheckIsTemporary
- Following both Unwrap() error and Unwrap() []error patterns
- Detecting legacy net.Error.Temporary() implementations
- Detecting modern IsTemporary() bool implementations
- Detecting timeout errors (which are also considered temporary)
Returns true if any error in the chain indicates a temporary condition. Returns false for nil errors or chains with no temporary indicators.
Use this function when you need to determine if an operation should be retried based on the error's temporary nature.
func IsTimeout ¶ added in v0.13.2
IsTimeout tests an error chain for timeout conditions recursively. It traverses wrapped error chains using IsErrorFn2 to find any error that implements timeout condition interfaces.
This function provides comprehensive timeout error detection by:
- Checking each error in the unwrapping chain via CheckIsTimeout
- Following both Unwrap() error and Unwrap() []error patterns
- Detecting legacy net.Error.Timeout() implementations
- Detecting modern IsTimeout() bool implementations
Returns true if any error in the chain indicates a timeout condition. Returns false for nil errors or chains with no timeout indicators.
Use this function when you need to distinguish timeout errors from other types of temporary errors for specialized retry logic or timeout-specific error handling.
func IsZero ¶ added in v0.9.8
IsZero reports whether vi is in a state where it can/should be initialized. It answers the question: "Is this value uninitialized and ready to be set?"
IsZero returns true for values that are in their uninitialized state:
- nil (untyped nil)
- Zero values of basic types (0, "", false)
- nil pointers, slices, maps, channels, and functions
- Zero-valued structs (all fields are zero)
- Values whose IsZero() method returns true
IsZero returns false for values that have been explicitly initialized:
- Non-zero basic values (42, "hello", true)
- Initialized empty collections ([]int{}, map[string]int{})
- Non-nil pointers (even if pointing to zero values)
- Non-zero structs (any field is non-zero)
The key insight: something set to nil but assignable is zero; something explicitly initialized (even if empty) is not zero.
Initialization semantics:
- var slice []int // nil slice, IsZero = true (needs initialization)
- slice := []int{} // empty but initialized, IsZero = false (already set)
- var m map[string]int // nil map, IsZero = true (needs initialization)
- m := make(map[string]int) // empty but initialized, IsZero = false (already set)
- var ptr *int // nil pointer, IsZero = true (can be assigned)
- ptr := new(int) // non-nil pointer, IsZero = false (already assigned)
Example:
IsZero(nil) // true - uninitialized
IsZero(0) // true - zero basic value
IsZero(42) // false - initialized basic value
IsZero("") // true - zero string
IsZero("hello") // false - initialized string
IsZero([]int(nil)) // true - nil slice (uninitialized)
IsZero([]int{}) // false - empty slice (initialized)
IsZero([]int{1, 2}) // false - non-empty slice (initialized)
IsZero((*int)(nil)) // true - nil pointer (uninitialized)
IsZero(new(int)) // false - non-nil pointer (initialized)
func JoinHostPort ¶ added in v0.14.3
JoinHostPort is like the standard net.JoinHostPort, but it validates the host name and port, and returns it portless if the port argument is empty.
Unlike net.JoinHostPort, this function:
- Validates hostname and port before joining
- Returns host without port if port is empty
- Properly handles IPv6 addresses with bracketing
- Supports international domain names
- Returns descriptive errors for invalid inputs
Examples:
- JoinHostPort("localhost", "8080") → "localhost:8080"
- JoinHostPort("localhost", "") → "localhost"
- JoinHostPort("192.168.1.1", "80") → "192.168.1.1:80"
- JoinHostPort("::1", "443") → "[::1]:443"
- JoinHostPort("::1", "") → "::1"
- JoinHostPort("example.com", "0") → "example.com:0" (port 0 allowed)
- JoinHostPort("invalid host", "80") → error
- JoinHostPort("example.com", "99999") → error (port out of range)
func Keys ¶ added in v0.13.5
func Keys[K comparable, T any](m map[K]T) []K
Keys returns the list of keys of a map
func ListContains ¶
func ListContains[T comparable](l *list.List, val T) bool
ListContains checks if a container/list contains an element
func ListContainsFn ¶
ListContainsFn checks if a container/list contains an element that satisfies a given function
func ListCopyFn ¶ added in v0.14.9
ListCopyFn makes a copy of a list using the given helper
func ListForEach ¶
ListForEach calls a function for each value until told to stop
func ListForEachBackward ¶
ListForEachBackward calls a function for each value until told to stop
func ListForEachBackwardElement ¶
ListForEachBackwardElement calls a function for each element until told to stop
func ListForEachElement ¶
ListForEachElement calls a function for each element until told to stop
func MakeHostPort ¶ added in v0.14.3
MakeHostPort produces a validated host:port from an input string optionally using the given default port when the string doesn't specify one. port 0 on the string input isn't considered valid.
Examples:
- MakeHostPort("localhost", 8080) → "localhost:8080"
- MakeHostPort("localhost:9000", 8080) → "localhost:9000"
- MakeHostPort("192.168.1.1", 80) → "192.168.1.1:80"
- MakeHostPort("::1", 443) → "[::1]:443"
- MakeHostPort("example.com", 0) → "example.com"
- MakeHostPort("example.com:0", 80) → error (port 0 invalid)
func MapAllListContains ¶
func MapAllListContains[K comparable, T comparable](m map[K]*list.List, v T) bool
MapAllListContains check if a value exists on any entry of the map
func MapAllListContainsFn ¶
MapAllListContainsFn check if a value exists on any entry of the map using a match function
func MapAllListForEach ¶
func MapAllListForEach[K comparable, T any](m map[K]*list.List, fn func(v T) bool)
MapAllListForEach calls a function for each value on all map entries until told to stop
func MapAllListForEachElement ¶
MapAllListForEachElement calls a function for each element on all map entries until told to stop
func MapContains ¶
func MapContains[K comparable](m map[K]any, key K) bool
MapContains tells if a given map contains a key. this helper is intended for switch/case conditions
func MapListAppend ¶
func MapListAppend[K comparable, T any](m map[K]*list.List, key K, v T)
MapListAppend adds a value at the end of the list of a map entry
func MapListAppendUnique ¶
func MapListAppendUnique[K comparable, T comparable](m map[K]*list.List, key K, v T)
MapListAppendUnique adds a value at the end of the list of a map entry if it's not already there
func MapListAppendUniqueFn ¶
func MapListAppendUniqueFn[K comparable, T any](m map[K]*list.List, key K, v T, eq func(T, T) bool)
MapListAppendUniqueFn adds a value at the end of the list of a map entry if it's not already there using a function to compare values
func MapListContains ¶
func MapListContains[K comparable, T comparable](m map[K]*list.List, key K, v T) bool
MapListContains checks if the list.List on a map contains an element
func MapListContainsFn ¶
func MapListContainsFn[K comparable, T any](m map[K]*list.List, key K, v T, eq func(T, T) bool) bool
MapListContainsFn checks if the list.List on a map contains an element using a match functions
func MapListCopy ¶ added in v0.14.9
func MapListCopy[T comparable](src map[T]*list.List) map[T]*list.List
MapListCopy duplicates a map containing a list.List
func MapListCopyFn ¶ added in v0.14.9
func MapListCopyFn[K comparable, V any](src map[K]*list.List, fn func(v V) (V, bool)) map[K]*list.List
MapListCopyFn duplicates a map containing a list.List but allows the element's values to be cloned by a helper function
func MapListForEach ¶
func MapListForEach[K comparable, T any](m map[K]*list.List, key K, fn func(v T) bool)
MapListForEach calls a function for each value on a map entry until told to stop
func MapListForEachElement ¶
MapListForEachElement calls a function for each element on a map entry until told to stop
func MapListInsert ¶
func MapListInsert[K comparable, T any](m map[K]*list.List, key K, v T)
MapListInsert adds a value at the front of the list of a map entry
func MapListInsertUnique ¶
func MapListInsertUnique[K comparable, T comparable](m map[K]*list.List, key K, v T)
MapListInsertUnique adds a value at the front of the list of a map entry if it's not already there
func MapListInsertUniqueFn ¶
func MapListInsertUniqueFn[K comparable, T any](m map[K]*list.List, key K, v T, eq func(va, vb T) bool)
MapListInsertUniqueFn adds a value at the front of the list of a map entry if it's not already there using a function to compare values
func MapValue ¶ added in v0.14.9
func MapValue[K comparable, V any](m map[K]V, key K, def V) (V, bool)
MapValue returns a value of an entry or a default if not found
func Maybe ¶ added in v0.18.0
Maybe returns the value, ignoring any error. This is useful when you want to proceed with a default or zero value regardless of whether an error occurred. Unlike Must, it never panics.
Example usage:
// Use empty string if ReadFile fails
content := Maybe(os.ReadFile("optional.txt"))
// Use zero value if parsing fails
count := Maybe(strconv.Atoi(userInput))
// Chain operations where errors are non-critical
data := Maybe(json.Marshal(obj))
func MaybeOK ¶ added in v0.18.0
MaybeOK returns the value, ignoring the ok flag. This is useful when you want to proceed with a default or zero value regardless of whether the operation succeeded. Unlike MustOK, it never panics.
Example usage:
// Use zero value if key doesn't exist in map value := MaybeOK(MapValue(m, "key", 0)) // Use zero value if type assertion fails str := MaybeOK(As[any, string](v)) // Chain operations where success is non-critical result := MaybeOK(someFunc())
func MaybeT ¶ added in v0.18.1
MaybeT returns the converted value if type conversion succeeds, otherwise returns the zero value of the target type. This is useful when you want to proceed with a default value regardless of whether the type conversion succeeded. Unlike MustT, it never panics.
Example usage:
// Use zero value if conversion fails str := MaybeT[string](value) // empty string if value is not a string num := MaybeT[int](value) // zero if value is not an int reader := MaybeT[io.Reader](value) // nil if value doesn't implement io.Reader
func Must ¶ added in v0.18.0
Must panics if err is not nil, otherwise returns value. This is useful for situations where errors should never occur, such as test setup or configuration loading. It follows the common Go pattern of Must* functions that panic on error. The panic includes proper stack traces pointing to the caller.
Example usage:
config := Must(loadConfig("config.json")) // panics if loadConfig returns error
conn := Must(net.Dial("tcp", "localhost:8080")) // panics if dial fails
data := Must(json.Marshal(obj)) // panics if marshal fails
func MustOK ¶ added in v0.18.0
MustOK panics if ok is false, otherwise returns value. This is useful for situations where operations should always succeed, such as accessing map values that are known to exist or type assertions that are guaranteed to be valid. It follows the common Go pattern of Must* functions that panic on failure. The panic includes proper stack traces pointing to the caller.
Example usage:
value := MustOK(MapValue(m, "key", 0)) // panics if key doesn't exist in map str := MustOK(As[any, string](v)) // panics if v is not a string result := MustOK(someFunc()) // panics if someFunc returns false for ok
revive:disable-next-line:flag-parameter
func MustT ¶ added in v0.18.1
MustT panics if type conversion fails, otherwise returns the converted value. This is useful for type conversions that should always succeed, such as casting values to interfaces they are known to implement or converting between compatible types. It follows the common Go pattern of Must* functions that panic on failure. The panic includes proper stack traces pointing to the caller.
Example usage:
str := MustT[string](value) // panics if value is not a string num := MustT[int](value) // panics if value is not an int reader := MustT[io.Reader](value) // panics if value doesn't implement io.Reader
func NewTemporaryError ¶ added in v0.13.2
NewTemporaryError returns an error that returns false to IsTimeout() and true to IsTemporary()
func NewTimeoutError ¶ added in v0.13.2
NewTimeoutError returns an error that returns true to IsTimeout() and IsTemporary()
func NewUnreachableError ¶ added in v0.16.0
NewUnreachableError creates a new annotated ErrUnreachable with callstack.
func NewUnreachableErrorf ¶ added in v0.16.0
NewUnreachableErrorf creates a new annotated ErrUnreachable with callstack.
func PanicWrapf ¶
PanicWrapf emits a PanicError wrapping an annotated error using a formatting string.
func ParseNetIP ¶
ParseNetIP turns a string into a net.IP
func RunBenchmark ¶ added in v0.18.0
RunBenchmark runs a benchmark with setup and execution phases. This standardizes benchmark patterns with proper timer management.
Example usage:
RunBenchmark(b, func() interface{} {
return setupTestData()
}, func(data interface{}) {
processData(data)
})
func RunConcurrentTest ¶ added in v0.18.0
RunConcurrentTest runs multiple goroutines and waits for completion. This standardizes concurrent testing patterns.
Example usage:
err := RunConcurrentTest(t, 10, func(id int) error {
// worker logic here
return nil
})
AssertNoError(t, err, "concurrent test should not fail")
func RunTestCases ¶ added in v0.18.0
RunTestCases runs a slice of test cases that implement the TestCase interface. This eliminates the boilerplate of looping through test cases and calling t.Run.
Example usage:
RunTestCases(t, testCases)
func S ¶ added in v0.18.0
func S[T any](v ...T) []T
S is a helper function for creating test slices in a more concise way. It takes variadic arguments and returns a slice of the same type. This is particularly useful in table-driven tests where many slice literals are used. The function accepts any type, including structs with non-comparable fields.
Example usage:
S(1, 2, 3) // []int{1, 2, 3}
S("a", "b", "c") // []string{"a", "b", "c"}
S[int]() // []int{}
S[string]() // []string{}
S(testCase{...}) // []testCase{...} (works with any struct)
func SliceAs ¶ added in v0.14.8
func SliceAs[T, V any](vv []T) []V
SliceAs returns a subset of a slice that could be cast into a different type
func SliceAsFn ¶ added in v0.14.8
SliceAsFn returns a subset of a slice that could be cast into a different type, using a helper function.
func SliceContains ¶
func SliceContains[T comparable](a []T, v T) bool
SliceContains tells if a slice contains a given element
func SliceContainsFn ¶
SliceContainsFn tells if a slice contains a given element according to the callback eq
func SliceCopy ¶ added in v0.14.0
func SliceCopy[T any](s []T) []T
SliceCopy makes a shallow copy of a given slice
func SliceCopyFn ¶
SliceCopyFn makes a copy of a slice, optionally modifying in-flight the items using a function. If no function is provided, the destination will be a shallow copy of the source slice.
func SliceEqual ¶ added in v0.13.5
func SliceEqual[T comparable](a, b []T) bool
SliceEqual tells if two slices are equal.
func SliceEqualFn ¶ added in v0.13.5
SliceEqualFn tells if two slices are equal using a comparing helper.
func SliceMap ¶ added in v0.14.1
SliceMap takes a []T1 and uses a function to produce a []T2 by processing each item on the source slice.
func SliceMinus ¶
func SliceMinus[T comparable](a, b []T) []T
SliceMinus returns a new slice containing only the elements of one slice not present on the second
func SliceMinusFn ¶
SliceMinusFn returns a new slice containing only elements of slice A that aren't on slice B according to the callback eq
func SliceP ¶ added in v0.18.5
SliceP returns the p-th percentile element from a slice. The slice is sorted in place (modifying the original slice) before calculating the percentile. p must be between 0.0 (minimum) and 1.0 (maximum). Returns the zero value for empty slices or when p is outside the valid range. This is commonly used for performance metrics and latency measurements to identify outliers while excluding the worst values.
Example:
responseTimes := []int{100, 150, 120, 450, 89, 2000, 110, 130, 140, 160}
p99 := SliceP(responseTimes, 0.99) // Returns the value at the 99th percentile
// Note: responseTimes is now sorted
func SliceRandom ¶ added in v0.9.2
SliceRandom returns a random element from a slice if the slice is empty it will return the zero value of the slice type and false
func SliceReplaceFn ¶
SliceReplaceFn replaces or skips entries in a slice
func SliceReverse ¶ added in v0.14.7
func SliceReverse[T any](x []T)
SliceReverse modifies a slice reversing the order of its elements.
func SliceReversed ¶ added in v0.14.7
func SliceReversed[T any](a []T) []T
SliceReversed returns a copy of the slice, in reverse order.
func SliceReversedFn ¶ added in v0.14.7
SliceReversedFn returns a modified copy of the slice, in reverse order.
func SliceSort ¶ added in v0.13.5
SliceSort sorts the slice x in ascending order as determined by the cmp function. This sort is not guaranteed to be stable. cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b.
func SliceSortFn ¶ added in v0.14.2
SliceSortFn sorts the slice x in ascending order as a less function. This sort is not guaranteed to be stable. less(a, b) should true when a < b
func SliceSortOrdered ¶ added in v0.14.2
func SliceSortOrdered[T Ordered](x []T)
SliceSortOrdered sorts the slice x of an Ordered type in ascending order.
func SliceUnique ¶
func SliceUnique[T comparable](a []T) []T
SliceUnique returns a new slice containing only unique elements
func SliceUniqueFn ¶
SliceUniqueFn returns a new slice containing only unique elements according to the callback eq
func SliceUniquify ¶
func SliceUniquify[T comparable](ptr *[]T) []T
SliceUniquify returns the same slice, reduced to only contain unique elements
func SliceUniquifyFn ¶
SliceUniquifyFn returns the same slice, reduced to only contain unique elements according to the callback eq
func SortedKeys ¶ added in v0.13.5
SortedKeys returns a sorted list of the keys of a map
func SortedValues ¶ added in v0.16.2
SortedValues returns a slice of values from the map, sorted by their keys
func SortedValuesCond ¶ added in v0.16.2
SortedValuesCond returns a slice of values from the map, sorted by their keys, filtered by the optional predicate function fn, preallocated to the size of the map.
func SortedValuesUnlikelyCond ¶ added in v0.16.2
SortedValuesUnlikelyCond returns a slice of values from the map, sorted by their keys, filtered by the optional predicate function fn, without preallocation.
func SplitAddrPort ¶ added in v0.10.1
SplitAddrPort splits a string containing an IP address and an optional port, and validates it. Returns the address as netip.Addr and port as uint16.
This function:
- Accepts IP addresses with optional port numbers
- Validates the address is a valid IPv4 or IPv6 address
- Validates the port is in range 1-65535 (0 if no port specified)
- Properly handles IPv6 addresses with brackets
- Returns zero values and error for invalid inputs
Examples:
- SplitAddrPort("192.168.1.1:8080") → (192.168.1.1, 8080, nil)
- SplitAddrPort("192.168.1.1") → (192.168.1.1, 0, nil)
- SplitAddrPort("[::1]:443") → (::1, 443, nil)
- SplitAddrPort("::1") → (::1, 0, nil)
- SplitAddrPort("127.0.0.1:80") → (127.0.0.1, 80, nil)
- SplitAddrPort("invalid") → error
- SplitAddrPort("192.168.1.1:99999") → error (port out of range)
func SplitHostPort ¶
SplitHostPort is like net.SplitHostPort but doesn't fail if the port isn't part of the string and it validates it if present. SplitHostPort will also validate the host is a valid IP or name
Unlike net.SplitHostPort, this function:
- Accepts hostport strings without port (returns empty port)
- Validates the host is a valid IP address or hostname
- Validates the port is a valid port number (1-65535)
- Properly handles IPv6 addresses with and without brackets
- Supports international domain names with punycode conversion
- Returns descriptive errors for invalid inputs
Examples:
- SplitHostPort("localhost:8080") → ("localhost", "8080", nil)
- SplitHostPort("localhost") → ("localhost", "", nil)
- SplitHostPort("192.168.1.1:80") → ("192.168.1.1", "80", nil)
- SplitHostPort("[::1]:443") → ("::1", "443", nil)
- SplitHostPort("::1") → ("::1", "", nil)
- SplitHostPort("example.com") → ("example.com", "", nil)
- SplitHostPort("Hello.世界") → ("hello.世界", "", nil)
- SplitHostPort("invalid host") → error
- SplitHostPort("example.com:99999") → error (port out of range)
func Unwrap ¶ added in v0.13.0
Unwrap unwraps one layer of a compound error, ensuring there are no nil entries.
func WithTimeout ¶ added in v0.15.5
WithTimeout is equivalent to context.WithDeadline but taking a duration instead of an absolute time.
If the duration is zero or negative the context won't expire.
func WithTimeoutCause ¶ added in v0.15.5
func WithTimeoutCause(parent context.Context, tio time.Duration, cause error) (context.Context, context.CancelFunc)
WithTimeoutCause is equivalent to context.WithDeadlineCause but taking a duration instead of an absolute time.
If the duration is zero or negative the context won't expire.
func Zero ¶ added in v0.9.8
func Zero[T any](_ *T) T
Zero returns the zero value of type T. It takes a pointer to T as a parameter to infer the type, but the pointer value itself is ignored.
This function is useful when you need to obtain the zero value of a generic type without having to declare a variable.
Example:
var p *int
zero := Zero(p) // returns 0
var s *string
empty := Zero(s) // returns ""
type Person struct { Name string; Age int }
var person *Person
zeroPerson := Zero(person) // returns Person{Name: "", Age: 0}
Types ¶
type CallStacker ¶
type CallStacker interface {
CallStack() Stack
}
CallStacker represents an object that can provide its call stack. Types implementing this interface can be used for stack trace collection and debugging purposes.
type Catcher ¶
type Catcher struct {
// contains filtered or unexported fields
}
Catcher is a runner that catches panics
type Complex ¶ added in v0.14.0
type Complex interface {
~complex64 | ~complex128
}
Complex is any complex numeric type.
type CompoundError ¶ added in v0.9.1
type CompoundError struct {
Errs []error
}
A CompoundError can contain more that one error
func (*CompoundError) Append ¶ added in v0.14.5
func (w *CompoundError) Append(err error, note string, args ...any) *CompoundError
Append adds an error to the collection, optionally annotating it with a formatted note. If err is nil and a note is provided, a new error is created from the note. If both err and note are non-empty, the error is wrapped with the note. Returns the updated CompoundError for method chaining.
func (*CompoundError) AppendError ¶ added in v0.9.1
func (w *CompoundError) AppendError(errs ...error) *CompoundError
AppendError adds an error to the collection, unwrapping other implementers of the Errors interface when possible. It returns the updated CompoundError for method chaining.
func (*CompoundError) AsError ¶ added in v0.9.1
func (w *CompoundError) AsError() error
AsError returns itself as an `error` when there are errors stored, and nil when there aren't
func (*CompoundError) Error ¶ added in v0.9.1
func (w *CompoundError) Error() string
func (*CompoundError) Errors ¶ added in v0.9.1
func (w *CompoundError) Errors() []error
Errors returns the contained slice of errors
func (*CompoundError) OK ¶ added in v0.18.0
func (w *CompoundError) OK() bool
OK tells when there are no errors stored
func (*CompoundError) Ok
deprecated
added in
v0.9.1
func (w *CompoundError) Ok() bool
Ok tells when there are no errors stored
Deprecated: Use OK() instead.
func (*CompoundError) Unwrap ¶ added in v0.13.0
func (w *CompoundError) Unwrap() []error
Unwrap returns the contained slice of errors
type ContextKey ¶
type ContextKey[T any] struct { // contains filtered or unexported fields }
ContextKey is a type-safe key for a context.Context value
func NewContextKey ¶
func NewContextKey[T any](name string) *ContextKey[T]
NewContextKey creates a new ContextKey bound to the specified type and friendly name
func (*ContextKey[T]) Get ¶
func (ck *ContextKey[T]) Get(ctx context.Context) (T, bool)
Get attempts to extract a value bound to this key in a context.Context For convenience this method will safely operate over a nil receiver.
func (*ContextKey[T]) GoString ¶
func (ck *ContextKey[T]) GoString() string
GoString renders this key in Go syntax for %v
type ErrGroup ¶ added in v0.11.0
ErrGroup handles a group of workers where all are canceled once one fails. As it's based on WaitGroup it also catches panics.
func (*ErrGroup) Cancel ¶ added in v0.11.0
Cancel initiates a shutdown of the group. The returned value indicates if it was the first time.
func (*ErrGroup) Cancelled ¶ added in v0.11.0
func (eg *ErrGroup) Cancelled() <-chan struct{}
Cancelled returns a channel marker to know when the Group has been cancelled and the shutdown has been initiated.
Cancelled() doesn't indicate all workers have finished, for that call ErrGroup.Wait or ErrGroup.Done.
func (*ErrGroup) Context ¶ added in v0.11.0
Context returns the cancellable context used with the workers
func (*ErrGroup) Done ¶ added in v0.11.1
func (eg *ErrGroup) Done() <-chan struct{}
Done returns a channel that gets closed when all workers have finished.
func (*ErrGroup) Go ¶ added in v0.11.0
Go spawns a worker and an optional shutdown routine to be invoked when the ErrGroup is cancelled, otherwise the provided context needs to be monitored and shutdown called.
func (*ErrGroup) GoCatch ¶ added in v0.11.0
func (eg *ErrGroup) GoCatch(run func(context.Context) error, catch func(context.Context, error) error)
GoCatch runs a worker on the Group, with a custom error handler.
func (*ErrGroup) IsCancelled ¶ added in v0.11.0
IsCancelled tells the ErrGroup has been cancelled
func (*ErrGroup) OnError ¶ added in v0.11.0
OnError sets a helper that will be called when a worker returns an error or panics
func (*ErrGroup) SetDefaults ¶ added in v0.11.0
func (eg *ErrGroup) SetDefaults()
SetDefaults fills gaps in the config and initializes the internal structure.
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame represents a single function call frame in a call stack. It captures function name, source file, line number, and program counter information at the time of stack capture.
This implementation is heavily based on github.com/pkg/errors.Frame but resolves all information immediately for efficient later consumption. Each Frame contains:
- Function name (package.function)
- Source file path
- Line number
- Program counter and entry point
func Here ¶
func Here() *Frame
Here captures and returns the current stack frame where it was called. This is useful for capturing the immediate calling context for debugging.
Returns nil if stack capture fails or if called in an environment where runtime stack information is unavailable.
Example:
frame := Here()
if frame != nil {
fmt.Printf("Called from %s at %s", frame.FuncName(), frame.FileLine())
}
func StackFrame ¶
StackFrame captures a specific frame in the call stack, skipping the specified number of stack levels above the caller.
Parameters:
- skip: number of stack frames to skip (0 = caller, 1 = caller's caller, etc.)
Returns nil if the stack doesn't have enough frames or if capture fails. Useful for creating stack-aware error reporting utilities.
Example:
// Get the frame 2 levels up the stack
frame := StackFrame(2)
if frame != nil {
log.Printf("Error originated from %s", frame.Name())
}
func (Frame) File ¶
File returns the full path to the source file containing this frame's function. Returns empty string for zero-valued frames or when file information is unavailable.
func (Frame) FileLine ¶
FileLine returns a formatted "file:line" string for this frame. If line number is available and positive, returns "filename:123". If line is zero or negative, returns just the filename. Useful for displaying source location in error messages and logs.
func (Frame) Format ¶
Format formats the frame according to the fmt.Formatter interface.
The following verbs are supported:
%s source file %d source line %n function name %v equivalent to %s:%d
Format accepts flags that alter the printing of some verbs:
%+s function name and path of source file relative to the compile time
GOPATH separated by \n\t (<funcName>\n\t<path>)
%+n full package name followed by function name
%+v equivalent to %+s:%d
%#s package/file format using PkgFile()
%#v equivalent to %#s:%d
func (Frame) FuncName ¶ added in v0.9.10
FuncName returns only the function name without package qualification. For example: "TestFunction" or "main". Uses SplitName internally to separate package from function name.
func (Frame) Line ¶
Line returns the line number within the source file for this frame. Returns 0 for zero-valued frames or when line information is unavailable.
func (Frame) Name ¶
Name returns the full qualified function name including package path. For example: "darvaza.org/core.TestFunction" or "main.main". Returns empty string for zero-valued frames.
func (Frame) PkgFile ¶ added in v0.17.5
PkgFile returns the package name (if present) followed by '/' and the file name. For example: "darvaza.org/core/stack.go". If no package name exists, returns just the file name. Returns empty string for zero-valued frames or when file information is unavailable.
func (Frame) PkgName ¶ added in v0.9.10
PkgName returns the package path portion of the function name. For example: "darvaza.org/core" or "main". Returns empty string if no package separator is found.
func (Frame) SplitName ¶ added in v0.9.10
SplitName splits the full function name into package and function components. Handles generic function names by ignoring trailing "[...]" suffixes. Searches for the last "." or "/" separator to determine the split point.
Returns:
- pkgName: package path ("darvaza.org/core")
- funcName: function name ("TestFunction")
If no separator is found, returns an empty package name and the full name as the function name.
type MockT ¶ added in v0.18.0
type MockT struct {
Errors []string
Logs []string
HelperCalled int
// contains filtered or unexported fields
}
MockT is a mock implementation of the T interface for testing purposes. It collects error and log messages instead of reporting them to the testing framework.
MockT supports all standard testing methods including Fatal/Fatalf which panic with a special error that can be caught by the Run method. This allows testing of assertion functions and other utilities that may call Fatal methods.
The Run method executes test functions and recovers from FailNow/Fatal panics, making it ideal for testing assertion functions where you need to verify both success and failure scenarios without terminating the test runner.
func (*MockT) Error ¶ added in v0.18.0
Error implements the T interface and collects error messages. It also marks the test as failed.
func (*MockT) Errorf ¶ added in v0.18.0
Errorf implements the T interface and collects formatted error messages. It also marks the test as failed.
func (*MockT) Fail ¶ added in v0.18.0
func (m *MockT) Fail()
Fail implements the T interface and marks the test as failed.
func (*MockT) FailNow ¶ added in v0.18.1
func (m *MockT) FailNow()
FailNow implements the T interface and marks the test as failed, then panics.
func (*MockT) Failed ¶ added in v0.18.0
Failed implements the T interface and returns whether the test has been marked as failed.
func (*MockT) Fatal ¶ added in v0.18.1
Fatal implements the T interface and collects error messages, then panics. It combines Error and FailNow functionality.
func (*MockT) Fatalf ¶ added in v0.18.1
Fatalf implements the T interface and collects formatted error messages, then panics. It combines Errorf and FailNow functionality.
func (*MockT) Helper ¶ added in v0.18.0
func (m *MockT) Helper()
Helper implements the T interface and tracks that it was called.
func (*MockT) LastError ¶ added in v0.18.0
LastError returns the last error message and whether one was found.
func (*MockT) LastLog ¶ added in v0.18.0
LastLog returns the last log message and whether one was found.
func (*MockT) Logf ¶ added in v0.18.0
Logf implements the T interface and collects formatted log messages.
func (*MockT) Reset ¶ added in v0.18.0
func (m *MockT) Reset()
Reset clears all recorded errors, logs, failed state, and helper state.
func (*MockT) Run ¶ added in v0.18.1
Run runs the test function f with the MockT instance and returns whether it passed. It recovers from FailNow/Fatal panics and returns false if the test failed or panicked. Non-FailNow panics are re-thrown. Returns false for nil MockT or nil function.
This method is ideal for testing assertion functions that may call Fatal/FailNow:
mock := &MockT{}
ok := mock.Run("test assertion", func(t T) {
AssertEqual(t, 1, 2, "should fail") // This calls t.Fatal internally
})
// ok == false, mock.Failed() == true, mock.Errors contains failure message
Unlike testing.T.Run, this method uses the same MockT instance throughout, allowing inspection of all collected errors, logs, and failure state after execution.
This Run method enables compatibility patterns where test functions need to work with both *testing.T and MockT. Use interface type assertions to detect Run method support:
func doRun(t core.T, name string, fn func(core.T)) {
switch tt := t.(type) {
case interface { Run(string, func(*testing.T)) bool }:
tt.Run(name, func(subT *testing.T) { fn(subT) })
case interface { Run(string, func(core.T)) bool }:
tt.Run(name, fn)
default:
fn(t) // Fallback for simple core.T implementations
}
}
type PanicError ¶
type PanicError struct {
// contains filtered or unexported fields
}
PanicError is an error to be sent via panic, ideally to be caught using slog.Recover()
func NewPanicError ¶
func NewPanicError(skip int, payload any) *PanicError
NewPanicError creates a new PanicError with arbitrary payload
func NewPanicErrorf ¶
func NewPanicErrorf(skip int, format string, args ...any) *PanicError
NewPanicErrorf creates a new PanicError annotated with a string, optionally formatted. %w is expanded.
func NewPanicWrap ¶
func NewPanicWrap(skip int, err error, note string) *PanicError
NewPanicWrap creates a new PanicError wrapping a given error annotated with a single string.
func NewPanicWrapf ¶
func NewPanicWrapf(skip int, err error, format string, args ...any) *PanicError
NewPanicWrapf creates a new PanicError wrapping a given error annotated with a formatted string.
func (*PanicError) CallStack ¶
func (p *PanicError) CallStack() Stack
CallStack returns the call stack associated to this panic() event
func (*PanicError) Error ¶
func (p *PanicError) Error() string
Error returns the payload as a string
func (*PanicError) Recovered ¶
func (p *PanicError) Recovered() any
Recovered returns the payload of the panic
func (*PanicError) Unwrap ¶
func (p *PanicError) Unwrap() error
Unwrap returns the payload if it's and error
type Recovered ¶
Recovered is an error caught from a panic call
func AsRecovered ¶
AsRecovered receives the value from recover() and wraps it as a Recovered error
type SpinLock
deprecated
added in
v0.15.3
type SpinLock uint32
SpinLock is a simple CompareAndSwap locking mechanism.
Deprecated: Use darvaza.org/x/sync/spinlock instead.
func (*SpinLock) Lock ¶ added in v0.15.3
func (sl *SpinLock) Lock()
Lock blocks until it can acquire the lock
type Stack ¶
type Stack []Frame
Stack represents a captured call stack as a slice of Frame objects. Each Frame in the stack corresponds to a function call, ordered from the most recent call (index 0) to the oldest call.
Stack implements custom formatting via the Format method, supporting various output formats for debugging and logging purposes.
func StackTrace ¶
StackTrace captures a complete call stack starting from the specified skip level. The resulting Stack contains frames ordered from most recent to oldest call.
Parameters:
- skip: number of initial stack frames to skip before capture begins
Returns an empty Stack if capture fails or if there are insufficient frames. The maximum capture depth is limited by MaxDepth (32 frames).
This function is commonly used for error reporting, debugging, and logging where complete call context is needed.
Example:
// Capture stack excluding this function and its caller
stack := StackTrace(2)
for i, frame := range stack {
fmt.Printf("[%d] %s at %s", i, frame.Name(), frame.FileLine())
}
func (Stack) Format ¶
Format formats the entire stack using the same verbs as Frame.Format with additional support for the '#' flag for numbered output.
Supported format verbs (same as Frame.Format):
%s, %d, %n, %v - basic formatting %+s, %+n, %+v - verbose formatting with full paths/names
Additional '#' flag support:
%#s, %#n, %#v - each frame on new line %#+s, %#+n, %#+v - numbered frames with [index/total] prefix
Example outputs:
fmt.Printf("%+v", stack) - multi-line stack with full info
fmt.Printf("%#+v", stack) - numbered multi-line stack
fmt.Printf("%#n", stack) - function names only, numbered
type T ¶ added in v0.18.0
type T interface {
Helper()
Error(args ...any)
Errorf(format string, args ...any)
Fatal(args ...any)
Fatalf(format string, args ...any)
Log(args ...any)
Logf(format string, args ...any)
Fail()
FailNow()
Failed() bool
}
T is an interface that abstracts the testing functionality we need. This allows our testing utilities to work with both *testing.T and mock implementations.
type TemporaryError ¶ added in v0.13.2
type TemporaryError struct {
// contains filtered or unexported fields
}
TemporaryError is an error wrapper that satisfies IsTimeout() and IsTemporary()
func (*TemporaryError) Error ¶ added in v0.13.2
func (w *TemporaryError) Error() string
func (*TemporaryError) IsTemporary ¶ added in v0.13.2
func (*TemporaryError) IsTemporary() bool
IsTemporary tells this error is temporary.
func (*TemporaryError) IsTimeout ¶ added in v0.13.2
func (w *TemporaryError) IsTimeout() bool
IsTimeout tells if this error is a time-out or not.
func (*TemporaryError) Temporary ¶ added in v0.13.2
func (*TemporaryError) Temporary() bool
Temporary tells this error is temporary.
func (*TemporaryError) Timeout ¶ added in v0.13.2
func (w *TemporaryError) Timeout() bool
Timeout tells if this error is a time-out or not.
type TestCase ¶ added in v0.18.0
TestCase represents a test case that can be executed. This interface is used by RunTestCases to standardize test case execution.
type Unwrappable ¶
Unwrappable represents an error that can be Unwrap() to get the cause
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup is a safer way to run workers
func (*WaitGroup) Done ¶ added in v0.11.1
func (wg *WaitGroup) Done() <-chan struct{}
Done returns a channel that gets closed when all workers have finished.
func (*WaitGroup) GoCatch ¶
GoCatch spawns a supervised goroutine, and uses a given function to intercept the returned error
type WrappedError ¶
type WrappedError struct {
// contains filtered or unexported fields
}
WrappedError is an annotated error that can be Unwrapped
func (*WrappedError) Error ¶
func (w *WrappedError) Error() string
func (*WrappedError) Unwrap ¶
func (w *WrappedError) Unwrap() error