types
Common interface contracts and function adapters used across the foomo ecosystem. Each lifecycle interface ships with a small family of adapters so plain functions can satisfy it without writing wrapper structs.
Import
import "github.com/foomo/go/types"Pattern
For each context-aware interface (e.g. Starter), the package provides:
<Name>Func— adaptsfunc()<Name>FuncErr— adaptsfunc() error<Name>FuncCtx— adaptsfunc(context.Context)<Name>FuncCtxErr— adaptsfunc(context.Context) errorAs<Name>(v any) (<Name>, bool)— runtime type assertion that promotes any of the above (or an existing<Name>) into the interface
The function adapters never return errors when the underlying function has none, so wrapping a func() in <Name>Func is always safe.
Interfaces
Closer
type Closer interface {
Close(ctx context.Context) error
}Adapters: CloseFunc, CloseFuncErr, CloseFuncCtx, CloseFuncCtxErr. Helper: AsCloser.
Pinger
type Pinger interface {
Ping(ctx context.Context) error
}Adapters: PingFunc, PingFuncErr, PingFuncCtx, PingFuncCtxErr. Helper: AsPinger.
Shutdowner
type Shutdowner interface {
Shutdown(ctx context.Context) error
}Adapters: ShutdownFunc, ShutdownFuncErr, ShutdownFuncCtx, ShutdownFuncCtxErr. Helper: AsShutdowner.
Starter
type Starter interface {
Start(ctx context.Context) error
}Adapters: StartFunc, StartFuncErr, StartFuncCtx, StartFuncCtxErr. Helper: AsStarter.
Stopper
type Stopper interface {
Stop(ctx context.Context) error
}Adapters: StopFunc, StopFuncErr, StopFuncCtx, StopFuncCtxErr. Helper: AsStopper.
Unsubscriber
type Unsubscriber interface {
Unsubscribe(ctx context.Context) error
}Adapters: UnsubscribeFunc, UnsubscribeFuncErr, UnsubscribeFuncCtx, UnsubscribeFuncCtxErr. Helper: AsUnsubscriber.
Namer
type Namer interface {
Name() string
}Stringer
type Stringer interface {
String() string
}Example
package main
import (
"context"
"fmt"
"github.com/foomo/go/types"
)
func main() {
// Any of these forms can be promoted to a Starter.
candidates := []any{
func() { fmt.Println("plain") },
func() error { fmt.Println("err"); return nil },
func(ctx context.Context) { fmt.Println("ctx") },
func(ctx context.Context) error { fmt.Println("ctx+err"); return nil },
}
for _, c := range candidates {
s, ok := types.AsStarter(c)
if !ok {
continue
}
_ = s.Start(context.Background())
}
}