Multiple writer, multiple listener message channel in Go.
- Any message from any writer goes to all listeners
- Supports adding and removing writers and listeners at runtime
m := multicast.New[string]()
defer m.Close()
listener, close := m.NewListener(listenerCapacity)
defer close(time.Second * 5)
go func() {
defer listener.Done()
for message := range listener.C() {
// ... do something useful ...
}
}()
writer, close := m.NewWriter()
defer close()
err := writer.Write(payload)
if err != nil {
// ... handle error ...
}