tree: 285a49a531fea8b8abb39d29ed4952499dc21d6c [path history] [tgz]
  1. aliasshim/
  2. apiv1/
  3. internal/
  4. loadtest/
  5. pstest/
  6. testdata/
  7. v2/
  8. .repo-metadata.json
  9. CHANGES.md
  10. debug.go
  11. doc.go
  12. example_subscription_iterator_test.go
  13. example_test.go
  14. example_topic_iterator_test.go
  15. flow_controller.go
  16. flow_controller_test.go
  17. go.mod
  18. go.sum
  19. integration_test.go
  20. iterator.go
  21. iterator_test.go
  22. message.go
  23. MIGRATING.md
  24. mock_test.go
  25. nodebug.go
  26. pstest_test.go
  27. pubsub.go
  28. pubsub_test.go
  29. pullstream.go
  30. pullstream_test.go
  31. README.md
  32. schema.go
  33. schema_test.go
  34. service.go
  35. snapshot.go
  36. streaming_pull_test.go
  37. subscription.go
  38. subscription_test.go
  39. timeout_test.go
  40. topic.go
  41. topic_test.go
  42. trace.go
  43. trace_test.go
  44. transform.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)
}