Go Channel

// ...
// GoChannel is the simplest Pub/Sub implementation.
// It is based on Golang's channels which are sent within the process.
//
// GoChannel has no global state,
// that means that you need to use the same instance for Publishing and Subscribing!
//
// When GoChannel is persistent, messages order is not guaranteed.
type GoChannel struct {
// ...

Full source: github.com/ThreeDotsLabs/watermill/pubsub/gochannel/pubsub.go

You can find a fully functional example with Go Channels in the Watermill examples .

Characteristics

FeatureImplementsNote
ConsumerGroupsno
ExactlyOnceDeliveryyes
GuaranteedOrderyes
Persistentno

Configuration

You can inject configuration via the constructor.

// ...
func NewGoChannel(config Config, logger watermill.LoggerAdapter) *GoChannel {
	if logger == nil {
		logger = watermill.NopLogger{}
	}

	return &GoChannel{
		config: config,

		subscribers:            make(map[string][]*subscriber),
		subscribersByTopicLock: sync.Map{},
		logger: logger.With(watermill.LogFields{
// ...

Full source: github.com/ThreeDotsLabs/watermill/pubsub/gochannel/pubsub.go

Publishing

// ...
// Publish in GoChannel is NOT blocking until all consumers consume.
// Messages will be send in background.
//
// Messages may be persisted or not, depending of persistent attribute.
func (g *GoChannel) Publish(topic string, messages ...*message.Message) error {
// ...

Full source: github.com/ThreeDotsLabs/watermill/pubsub/gochannel/pubsub.go

Subscribing

// ...
// Subscribe returns channel to which all published messages are sent.
// Messages are not persisted. If there are no subscribers and message is produced it will be gone.
//
// There are no consumer groups support etc. Every consumer will receive every produced message.
func (g *GoChannel) Subscribe(ctx context.Context, topic string) (<-chan *message.Message, error) {
// ...

Full source: github.com/ThreeDotsLabs/watermill/pubsub/gochannel/pubsub.go

Marshaler

No marshaling is needed when sending messages within the process.


Check our online hands-on training