本文整理匯總了Golang中cloud/google/com/go/pubsub.Client.Subscription方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Subscription方法的具體用法?Golang Client.Subscription怎麽用?Golang Client.Subscription使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cloud/google/com/go/pubsub.Client
的用法示例。
在下文中一共展示了Client.Subscription方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: delete
func delete(client *pubsub.Client, name string) error {
ctx := context.Background()
// [START delete_subscription]
sub := client.Subscription(name)
if err := sub.Delete(ctx); err != nil {
return err
}
fmt.Println("Subscription deleted.")
// [END delete_subscription]
return nil
}
示例2: getPolicy
func getPolicy(c *pubsub.Client, subName string) *iam.Policy {
ctx := context.Background()
// [START pubsub_get_subscription_policy]
policy, err := c.Subscription(subName).IAM().Policy(ctx)
if err != nil {
log.Fatal(err)
}
for _, role := range policy.Roles() {
log.Printf("%q: %q", role, policy.Members(role))
}
// [END pubsub_get_subscription_policy]
return policy
}
示例3: testPermissions
func testPermissions(c *pubsub.Client, subName string) []string {
ctx := context.Background()
// [START pubsub_test_subscription_permissions]
sub := c.Subscription(subName)
perms, err := sub.IAM().TestPermissions(ctx, []string{
"pubsub.subscriptions.consume",
"pubsub.subscriptions.update",
})
if err != nil {
log.Fatal(err)
}
for _, perm := range perms {
log.Printf("Allowed: %v", perm)
}
// [END pubsub_test_subscription_permissions]
return perms
}
示例4: addUsers
func addUsers(c *pubsub.Client, subName string) {
ctx := context.Background()
// [START pubsub_set_subscription_policy]
sub := c.Subscription(subName)
policy, err := sub.IAM().Policy(ctx)
if err != nil {
log.Fatalf("GetPolicy: %v", err)
}
// Other valid prefixes are "serviceAccount:", "user:"
// See the documentation for more values.
policy.Add(iam.AllUsers, iam.Viewer)
policy.Add("group:[email protected]", iam.Editor)
if err := sub.IAM().SetPolicy(ctx, policy); err != nil {
log.Fatalf("SetUser: %v", err)
}
// NOTE: It may be necessary to retry this operation if IAM policies are
// being modified concurrently. SetPolicy will return an error if the policy
// was modified since it was retrieved.
// [END pubsub_set_subscription_policy]
}
示例5: pullMsgs
func pullMsgs(client *pubsub.Client, name string, topic *pubsub.Topic) error {
ctx := context.Background()
// publish 10 messages on the topic.
for i := 0; i < 10; i++ {
_, err := topic.Publish(ctx, &pubsub.Message{
Data: []byte(fmt.Sprintf("hello world #%d", i)),
})
if err != nil {
return err
}
}
// [START pull_messages]
sub := client.Subscription(name)
it, err := sub.Pull(ctx)
if err != nil {
return err
}
defer it.Stop()
// Consume 10 messages.
for i := 0; i < 10; i++ {
msg, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
fmt.Printf("Got message: %q\n", string(msg.Data))
msg.Done(true)
}
// [END pull_messages]
return nil
}