gocloud.devgocloud.dev/pubsub/mempubsub Index | Examples | Files

package mempubsub

import "gocloud.dev/pubsub/mempubsub"

Package mempubsub provides an in-memory pubsub implementation. Use NewTopic to construct a *pubsub.Topic, and/or NewSubscription to construct a *pubsub.Subscription.

mempubsub should not be used for production: it is intended for local development and testing.

As

mempubsub does not support any types for As.

Example

Code:play 

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"gocloud.dev/pubsub"
	"gocloud.dev/pubsub/mempubsub"
)

func main() {
	// Construct a *pubsub.Topic.
	ctx := context.Background()
	t := mempubsub.NewTopic()
	defer t.Shutdown(ctx)

	// Construct a *pubsub.Subscription for the topic.
	s := mempubsub.NewSubscription(t, 1*time.Minute /* ack deadline */)
	defer s.Shutdown(ctx)

	// Now we can use t to send messages and s will receive them.
	err := t.Send(ctx, &pubsub.Message{Body: []byte("Hello World")})
	if err != nil {
		log.Fatal(err)
	}

	msg, err := s.Receive(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(msg.Body))
	msg.Ack()

}

Output:

Hello World

Index

Examples

Functions

func NewSubscription

func NewSubscription(top *pubsub.Topic, ackDeadline time.Duration) *pubsub.Subscription

NewSubscription creates a new subscription for the given topic. It panics if the given topic did not come from mempubsub.

func NewTopic

func NewTopic() *pubsub.Topic

NewTopic creates a new in-memory topic.

Source Files

mem.go

Version
v0.10.0
Published
Feb 12, 2019
Platform
js/wasm
Imports
7 packages
Last checked
1 week ago

Tools for package owners.