| 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. |