Compare commits
2 commits
0a0c185c06
...
59539c7e97
Author | SHA1 | Date | |
---|---|---|---|
59539c7e97 | |||
18bf623114 |
5 changed files with 17 additions and 4 deletions
|
@ -1,4 +1,4 @@
|
||||||
package middleware
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
|
@ -1,4 +1,4 @@
|
||||||
package middleware
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
|
@ -1,4 +1,4 @@
|
||||||
package other
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package middleware
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
|
@ -1,5 +1,7 @@
|
||||||
package other
|
package other
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// OutputIntoChannel takes a singular return value and sends it into the target channel.
|
// 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.
|
// This is a wrapper for functions where a value can't be collected from directly.
|
||||||
// Example: goroutines
|
// Example: goroutines
|
||||||
|
@ -7,6 +9,10 @@ func OutputIntoChannel[T any](out T, target chan T) {
|
||||||
target <- out
|
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 {
|
func Must[T any](val T, err error) T {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -14,6 +20,13 @@ func Must[T any](val T, err error) T {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IntoPointer returns a pointer to the given value
|
||||||
func IntoPointer[T any](val T) *T {
|
func IntoPointer[T any](val T) *T {
|
||||||
return &val
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue