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