Debian Codegraph @

pubsub/gcppubsubv2/example_test.go

raw
1 // Copyright 2025 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package gcppubsubv2_test
16
17 import (
18 "context"
19 "log"
20
21 "gocloud.dev/gcp"
22 "gocloud.dev/pubsub"
23 "gocloud.dev/pubsub/gcppubsubv2"
24 )
25
26 func ExampleOpenTopic() {
27 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
28 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
29 ctx := context.Background()
30
31 // Your GCP credentials.
32 // See https://cloud.google.com/docs/authentication/production
33 // for more info on alternatives.
34 creds, err := gcp.DefaultCredentials(ctx)
35 if err != nil {
36 log.Fatal(err)
37 }
38 // Open a gRPC connection to the GCP Pub/Sub API.
39 // Alternatively, skip this and pass nil to Client below to use the default.
40 conn, cleanup, err := gcppubsubv2.Dial(ctx, creds.TokenSource)
41 if err != nil {
42 log.Fatal(err)
43 }
44 defer cleanup()
45
46 // Construct a Client using the connection.
47 client, err := gcppubsubv2.Client(ctx, "myprojectid", conn)
48 if err != nil {
49 log.Fatal(err)
50 }
51 defer client.Close()
52
53 // Construct a *pubsub.Topic.
54 topic := gcppubsubv2.OpenTopic(client, "example-topic", nil)
55 defer topic.Shutdown(ctx)
56 }
57
58 func Example_openTopicFromURL() {
59 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
60 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/gcppubsubv2"
61 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
62 ctx := context.Background()
63
64 topic, err := pubsub.OpenTopic(ctx, "gcppubsubv2://projects/myproject/topics/mytopic")
65 if err != nil {
66 log.Fatal(err)
67 }
68 defer topic.Shutdown(ctx)
69 }
70
71 func ExampleOpenSubscription() {
72 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
73 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
74 ctx := context.Background()
75
76 // Your GCP credentials.
77 // See https://cloud.google.com/docs/authentication/production
78 // for more info on alternatives.
79 creds, err := gcp.DefaultCredentials(ctx)
80 if err != nil {
81 log.Fatal(err)
82 }
83
84 // Open a gRPC connection to the GCP Pub/Sub API.
85 // Alternatively, skip this and pass nil to Client below to use the default.
86 conn, cleanup, err := gcppubsubv2.Dial(ctx, creds.TokenSource)
87 if err != nil {
88 log.Fatal(err)
89 }
90 defer cleanup()
91
92 // Construct a Client using the connection.
93 client, err := gcppubsubv2.Client(ctx, "myprojectID", conn)
94 if err != nil {
95 log.Fatal(err)
96 }
97 defer client.Close()
98
99 // Construct a *pubsub.Subscription.
100 subscription := gcppubsubv2.OpenSubscription(client, "example-subscription", nil)
101 defer subscription.Shutdown(ctx)
102 }
103
104 func Example_openSubscriptionFromURL() {
105 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
106 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/gcppubsubv2"
107 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
108 ctx := context.Background()
109
110 subscription, err := pubsub.OpenSubscription(ctx,
111 "gcppubsubv2://projects/my-project/subscriptions/my-subscription")
112 if err != nil {
113 log.Fatal(err)
114 }
115 defer subscription.Shutdown(ctx)
116 }