From 59539c7e975e98d7eab628a3352ebb7dba81c484 Mon Sep 17 00:00:00 2001 From: mstar Date: Tue, 4 Mar 2025 13:57:51 +0100 Subject: [PATCH] Add error formatter and docs --- other/other.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/other/other.go b/other/other.go index 0c0b37e..f63b0b0 100644 --- a/other/other.go +++ b/other/other.go @@ -1,5 +1,7 @@ package other +import "fmt" + // OutputIntoChannel takes a singular return value and sends it into the target channel. // This is a wrapper for functions where a value can't be collected from directly. // Example: goroutines @@ -7,6 +9,10 @@ func OutputIntoChannel[T any](out T, target chan T) { target <- out } +// Must is a quick wrapper to ensure that a function exits without error. +// If err is not nil, it panics. Otherwise val is returned. +// The intended use is something like Must(someFunc()), where someFunc +// has the signature someFunc() (T, error), with T being whatever type needed func Must[T any](val T, err error) T { if err != nil { panic(err) @@ -14,6 +20,13 @@ func Must[T any](val T, err error) T { return val } +// IntoPointer returns a pointer to the given value func IntoPointer[T any](val T) *T { return &val } + +// Error formats error messages to follow a common convention of +// "source: message: wrapped error" +func Error(source, message string, err error) error { + return fmt.Errorf("%s: %s: %w", source, message, err) +}