Documentation
¶
Index ¶
- func Add[T Arithmetic](a, b T) T
- func Annotate(err error, ctx string) error
- func Annotatef(err error, format string, args ...any) error
- func BitAnd[T Integer](a, b T) T
- func BitOr[T Integer](a, b T) T
- func Check(err error)
- func CleanErrors[T any](value T, err error) (T, error)
- func Compose[X, Y, Z any](f func(X) Y, g func(Y) Z) func(X) Z
- func Concat[T ~string](a, b T) T
- func Mul[T Arithmetic](a, b T) T
- func Must[T any](value T, err error) T
- func Or[T comparable](items ...T) T
- func Pick[T any](condition bool, trueValue, falseValue T) T
- type Arithmetic
- type Complex
- type Integer
- type Real
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶ added in v0.0.6
func Add[T Arithmetic](a, b T) T
Add is a binary operation returning the sum of two Arithmetic values.
func Annotate ¶
Annotate adds extra context to an error message, essentially via a call to fmt.Errorf("%s: %w", ctx, err), but if the error was nil, no wrapped error is prodduced and nil is returned, allowing for clean returns from functions.
func Annotatef ¶
Annotatef is like annotate, but accepting Printf style arguments to generate the context message. Like with Annotate, no work is done if the error was nil, and a nil is simply returned.
func BitAnd ¶ added in v0.0.6
func BitAnd[T Integer](a, b T) T
BitAnd is a binary operation that does a bitwise and of two integral values.
func BitOr ¶ added in v0.0.6
func BitOr[T Integer](a, b T) T
BitOr is a binary operation that does a bitwise or of two integral values.
func CleanErrors ¶
CleanErrors wraps the return values from a function of the form (value,error) and ensures only one of the value or error is returned. If the error is non-nil, then a zero value is returned for value. This makes it easier to have "clean" return values where it doesn't make sense for the user to proceed when there is an error.
func Compose ¶
func Compose[X, Y, Z any](f func(X) Y, g func(Y) Z) func(X) Z
Compose builds a composite function from the to input functions f, and g. The returned function computes g(f(x)).
func Concat ¶ added in v0.0.6
func Concat[T ~string](a, b T) T
Concat is a binary operation that concatenates two string values.
func Mul ¶ added in v0.0.6
func Mul[T Arithmetic](a, b T) T
Mul is a binary operation returning the product of two Arithmetic values.
func Must ¶
Must accepts a value/error pair, and returns the value as long as the error is nil. If there is a non-nil error Must will panic with it.
func Or ¶
func Or[T comparable](items ...T) T
Or returns the first item from its list of arguments that isn't a zero value and returns zero if none are found.
Note: this is likely to be added to the stdlib as cmp.Or in go 1.22.
func Pick ¶
Pick returns trueValue if condition is true, falseValue otherwise. This allows for simple selection of a value based on a condition that would normally result in a 4 line (or more) code block due to the lack of a ternary operator in Go.
Unlike the ?: operator in C, since this is still a function, any computation needed to create the values passed as trueValue and falseValue are likely still performed as it's not guaranteed to be a short-circuit. Inlining may still produce benefits, but any side effects of producing the values will always happen, regardless of which value is selected by the condition.
Types ¶
type Arithmetic ¶ added in v0.0.6
Arithmetic is a type constraint for all types that support the basic arithmetic operations.
type Complex ¶ added in v0.0.6
type Complex interface {
~complex64 | ~complex128
}
Complex is a type constraint for all floating point complex types.