Minibus executes a set of functions concurrently and exchanges messages between
them. You can think of it like an errgroup.Group
with a built-in message
bus.
A more detailed version of this example annotated with comments is available here.
type SayHello struct {
Name string
}
minibus.Run(
context.Background(),
minibus.WithFunc(
func(ctx context.Context) error {
minibus.Subscribe[SayHello](ctx)
minibus.Ready(ctx)
for m := range minibus.Inbox(ctx) {
switch m := m.(type) {
case SayHello:
fmt.Printf("Hello, %s!\n", m.Name)
}
}
return nil
},
),
minibus.WithFunc(
func(ctx context.Context) error {
minibus.Ready(ctx)
return minibus.Send(ctx, SayHello{"world"})
},
),
)