本文整理匯總了Golang中cloud/google/com/go/pubsub.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ExampleSubscription_Pull
func ExampleSubscription_Pull() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
sub := client.Subscription("subName")
it, err := sub.Pull(ctx)
if err != nil {
// TODO: Handle error.
}
// Ensure that the iterator is closed down cleanly.
defer it.Stop()
// Consume 10 messages.
for i := 0; i < 10; i++ {
m, err := it.Next()
if err == pubsub.Done {
// There are no more messages. This will happen if it.Stop is called.
break
}
if err != nil {
// TODO: Handle error.
break
}
fmt.Printf("message %d: %s\n", i, m.Data)
// Acknowledge the message.
m.Done(true)
}
}
示例2: ExampleClient_Topics
func ExampleClient_Topics() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
it := client.Topics(ctx)
_ = it // TODO: iterate using Next.
}
示例3: ExampleNewClient
func ExampleNewClient() {
ctx := context.Background()
_, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
// See the other examples to learn how to use the Client.
}
示例4: ExampleClient_Subscriptions
func ExampleClient_Subscriptions() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
// List all subscriptions of the project.
it := client.Subscriptions(ctx)
_ = it // See the SubscriptionIterator example for its usage.
}
示例5: ExampleClient_Subscriptions
func ExampleClient_Subscriptions() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
// List all subscriptions of the project.
it := client.Subscriptions(ctx)
_ = it // TODO: iterate using Next.
}
示例6: setup
func setup(t *testing.T) *pubsub.Client {
ctx := context.Background()
tc := testutil.SystemTest(t)
client, err := pubsub.NewClient(ctx, tc.ProjectID)
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
return client
}
示例7: ExampleClient_Topics
func ExampleClient_Topics() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
// List all topics.
it := client.Topics(ctx)
_ = it // See the TopicIterator example for its usage.
}
示例8: ExampleSubscription_ModifyPushConfig
func ExampleSubscription_ModifyPushConfig() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
sub := client.Subscription("subName")
if err := sub.ModifyPushConfig(ctx, &pubsub.PushConfig{Endpoint: "https://example.com/push"}); err != nil {
// TODO: Handle error.
}
}
示例9: ExampleTopic_Delete
func ExampleTopic_Delete() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
topic := client.Topic("topicName")
if err := topic.Delete(ctx); err != nil {
// TODO: Handle error.
}
}
示例10: ExampleSubscription_Delete
func ExampleSubscription_Delete() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
sub := client.Subscription("subName")
if err := sub.Delete(ctx); err != nil {
// TODO: Handle error.
}
}
示例11: ExampleSubscription_Pull
func ExampleSubscription_Pull() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
it, err := client.Subscription("subName").Pull(ctx)
if err != nil {
// TODO: Handle error.
}
// Ensure that the iterator is closed down cleanly.
defer it.Stop()
}
示例12: ExampleSubscription_Config
func ExampleSubscription_Config() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
sub := client.Subscription("subName")
config, err := sub.Config(ctx)
if err != nil {
// TODO: Handle error.
}
fmt.Println(config)
}
示例13: ExampleClient_NewTopic
func ExampleClient_NewTopic() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
// Create a new topic with the given name.
topic, err := client.CreateTopic(ctx, "topicName")
if err != nil {
// TODO: Handle error.
}
_ = topic // TODO: use the topic.
}
示例14: main
func main() {
ctx := context.Background()
pubsubClient, err := pubsub.NewClient(ctx, "project")
if err != nil {
log.Fatalf("pubsub.NewClient: %v", err)
}
topic, err := pubsubClient.NewTopic(ctx, "topic")
if err != nil {
log.Fatalf("NewTopic: %v", err)
}
sub, err := pubsubClient.NewSubscription(ctx, "sub", topic, 0, nil)
if err != nil {
log.Fatalf("NewSubscription: %v", err)
}
if _, err := topic.Publish(ctx, &pubsub.Message{
Data: []byte("hello"),
}); err != nil {
log.Fatalf("Publish: %v", err)
}
it, err := sub.Pull(ctx)
if err != nil {
log.Fatalf("Pull: %v", err)
}
msg, err := it.Next()
if err != nil {
log.Fatalf("Next: %v", err)
}
log.Printf("pubsub message: %s", msg.Data)
msg.Done(true)
it.Stop()
datastoreClient, err := datastore.NewClient(ctx, "project")
if err != nil {
log.Fatalf("datastore.NewClient: %v", err)
}
k := datastore.NewIncompleteKey(ctx, "Foo", nil)
k, err = datastoreClient.Put(ctx, k, &Foo{F: "foo!"})
if err != nil {
log.Fatalf("Put: %v", err)
}
var f Foo
if err := datastoreClient.Get(ctx, k, &f); err != nil {
log.Fatalf("Get: %v", err)
}
log.Printf("datastore got %v", f)
}
示例15: ExampleSubscription_Exists
func ExampleSubscription_Exists() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
sub := client.Subscription("subName")
ok, err := sub.Exists(ctx)
if err != nil {
// TODO: Handle error.
}
if !ok {
// Subscription doesn't exist.
}
}