tree: 42e7d2213b0df659d8716a786ba3e6b1d997cb08 [search]
  1. apiv1/
  2. internal/
  3. CHANGES.md
  4. debug.go
  5. doc.go
  6. flow_controller.go
  7. iterator.go
  8. LICENSE
  9. message.go
  10. nodebug.go
  11. pubsub.go
  12. pullstream.go
  13. README.md
  14. schema.go
  15. service.go
  16. snapshot.go
  17. subscription.go
  18. topic.go
  19. trace.go
vendor/cloud.google.com/go/pubsub/README.md

Cloud Pub/Sub Go Reference

Example Usage

First create a pubsub.Client to use throughout your application:

client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
	log.Fatal(err)
}

Then use the client to publish and subscribe:

// Publish "hello world" on topic1.
topic := client.Topic("topic1")
res := topic.Publish(ctx, &pubsub.Message{
	Data: []byte("hello world"),
})
// The publish happens asynchronously.
// Later, you can get the result from res:
...
msgID, err := res.Get(ctx)
if err != nil {
	log.Fatal(err)
}

// Use a callback to receive messages via subscription1.
sub := client.Subscription("subscription1")
err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
	fmt.Println(m.Data)
	m.Ack() // Acknowledge that we've consumed the message.
})
if err != nil {
	log.Println(err)
}