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

Function ExampleOpenTopic

pubsub/gcppubsubv2/example_test.go:26–56  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

24)
25
26func 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
58func Example_openTopicFromURL() {
59 // 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
ClientFunction · 0.92
OpenTopicFunction · 0.92
CloseMethod · 0.65
ShutdownMethod · 0.65

Tested by

no test coverage detected