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