當前位置: 首頁>>代碼示例>>Golang>>正文


Golang pubsub.NewClient函數代碼示例

本文整理匯總了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)
	}
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:33,代碼來源:example_test.go

示例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.
}
開發者ID:rawlingsj,項目名稱:gofabric8,代碼行數:9,代碼來源:example_topic_iterator_test.go

示例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.
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:9,代碼來源:example_test.go

示例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.
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:10,代碼來源:example_test.go

示例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.
}
開發者ID:rawlingsj,項目名稱:gofabric8,代碼行數:10,代碼來源:example_subscription_iterator_test.go

示例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
}
開發者ID:GoogleCloudPlatform,項目名稱:golang-samples,代碼行數:10,代碼來源:main_test.go

示例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.
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:10,代碼來源:example_test.go

示例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.
	}
}
開發者ID:rawlingsj,項目名稱:gofabric8,代碼行數:11,代碼來源:example_test.go

示例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.
	}
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:12,代碼來源:example_test.go

示例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.
	}
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:12,代碼來源:example_test.go

示例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()
}
開發者ID:rawlingsj,項目名稱:gofabric8,代碼行數:13,代碼來源:example_test.go

示例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)
}
開發者ID:rawlingsj,項目名稱:gofabric8,代碼行數:13,代碼來源:example_test.go

示例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.
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:15,代碼來源:example_test.go

示例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)
}
開發者ID:broady,項目名稱:with_emulators,代碼行數:48,代碼來源:main.go

示例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.
	}
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:16,代碼來源:example_test.go


注:本文中的cloud/google/com/go/pubsub.NewClient函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。