Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
|
2 years ago | |
---|---|---|
.. | ||
apiv1 | 2 years ago | |
internal/distribution | 2 years ago | |
README.md | 4 years ago | |
debug.go | 4 years ago | |
doc.go | 4 years ago | |
flow_controller.go | 4 years ago | |
iterator.go | 4 years ago | |
message.go | 4 years ago | |
nodebug.go | 4 years ago | |
pubsub.go | 4 years ago | |
pullstream.go | 4 years ago | |
service.go | 4 years ago | |
snapshot.go | 4 years ago | |
subscription.go | 4 years ago | |
topic.go | 4 years ago | |
trace.go | 4 years ago |
First create a pubsub.Client
to use throughout your application:
client, err := pubsub.NewClient(ctx, "project-id")
if err != nil {
log.Fatal(err)
}
Then use the client to publish and subscribe:
// Publish "hello world" on topic1.
topic := client.Topic("topic1")
res := topic.Publish(ctx, &pubsub.Message{
Data: []byte("hello world"),
})
// The publish happens asynchronously.
// Later, you can get the result from res:
...
msgID, err := res.Get(ctx)
if err != nil {
log.Fatal(err)
}
// Use a callback to receive messages via subscription1.
sub := client.Subscription("subscription1")
err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
fmt.Println(m.Data)
m.Ack() // Acknowledge that we've consumed the message.
})
if err != nil {
log.Println(err)
}
No Description
Go SVG JavaScript Vue Roff other