1## Cloud Pub/Sub [![Go Reference](https://pkg.go.dev/badge/cloud.google.com/go/pubsub.svg)](https://pkg.go.dev/cloud.google.com/go/pubsub)
2
3- [About Cloud Pubsub](https://cloud.google.com/pubsub/)
4- [API documentation](https://cloud.google.com/pubsub/docs)
5- [Go client documentation](https://pkg.go.dev/cloud.google.com/go/pubsub)
6- [Complete sample programs](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/pubsub)
7
8### Example Usage
9
10First create a `pubsub.Client` to use throughout your application:
11
12[snip]:# (pubsub-1)
13```go
14client, err := pubsub.NewClient(ctx, "project-id")
15if err != nil {
16	log.Fatal(err)
17}
18```
19
20Then use the client to publish and subscribe:
21
22[snip]:# (pubsub-2)
23```go
24// Publish "hello world" on topic1.
25topic := client.Topic("topic1")
26res := topic.Publish(ctx, &pubsub.Message{
27	Data: []byte("hello world"),
28})
29// The publish happens asynchronously.
30// Later, you can get the result from res:
31...
32msgID, err := res.Get(ctx)
33if err != nil {
34	log.Fatal(err)
35}
36
37// Use a callback to receive messages via subscription1.
38sub := client.Subscription("subscription1")
39err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
40	fmt.Println(m.Data)
41	m.Ack() // Acknowledge that we've consumed the message.
42})
43if err != nil {
44	log.Println(err)
45}
46```
47