MCPcopy Create free account
hub / github.com/google/go-cloud / ExampleOpenTopic

Function ExampleOpenTopic

pubsub/gcppubsub/example_test.go:29–61  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27)
28
29func ExampleOpenTopic() {
30 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
31 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
32 ctx := context.Background()
33
34 // Your GCP credentials.
35 // See https://cloud.google.com/docs/authentication/production
36 // for more info on alternatives.
37 creds, err := gcp.DefaultCredentials(ctx)
38 if err != nil {
39 log.Fatal(err)
40 }
41 // Open a gRPC connection to the GCP Pub/Sub API.
42 conn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource)
43 if err != nil {
44 log.Fatal(err)
45 }
46 defer cleanup()
47
48 // Construct a PublisherClient using the connection.
49 pubClient, err := gcppubsub.PublisherClient(ctx, conn)
50 if err != nil {
51 log.Fatal(err)
52 }
53 defer pubClient.Close()
54
55 // Construct a *pubsub.Topic.
56 topic, err := gcppubsub.OpenTopicByPath(pubClient, "projects/myprojectID/topics/example-topic", nil)
57 if err != nil {
58 log.Fatal(err)
59 }
60 defer topic.Shutdown(ctx)
61}
62
63func Example_openTopicFromURL() {
64 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.

Callers

nothing calls this directly

Calls 6

DefaultCredentialsFunction · 0.92
DialFunction · 0.92
PublisherClientFunction · 0.92
OpenTopicByPathFunction · 0.92
CloseMethod · 0.65
ShutdownMethod · 0.65

Tested by

no test coverage detected