tree: af9479095e91241f802fa8ee9471da3760a10397 [path history] [tgz]
  1. .repo-metadata.json
  2. CHANGES.md
  3. README.md
  4. apiv1/
  5. debug.go
  6. doc.go
  7. example_subscription_iterator_test.go
  8. example_test.go
  9. example_topic_iterator_test.go
  10. flow_controller.go
  11. flow_controller_test.go
  12. go.mod
  13. go.sum
  14. go_mod_tidy_hack.go
  15. integration_test.go
  16. internal/
  17. iterator.go
  18. iterator_test.go
  19. loadtest/
  20. message.go
  21. mock_test.go
  22. nodebug.go
  23. pstest/
  24. pstest_test.go
  25. pubsub.go
  26. pullstream.go
  27. pullstream_test.go
  28. service.go
  29. snapshot.go
  30. streaming_pull_test.go
  31. subscription.go
  32. subscription_test.go
  33. timeout_test.go
  34. topic.go
  35. topic_test.go
  36. trace.go
pubsub/README.md

Cloud Pub/Sub GoDoc

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)
}