Skip to content

Instantly share code, notes, and snippets.

@sugamon
Created December 24, 2019 02:22
Show Gist options
  • Save sugamon/a0cd6be4d4774601ae08b5e25f262147 to your computer and use it in GitHub Desktop.
Save sugamon/a0cd6be4d4774601ae08b5e25f262147 to your computer and use it in GitHub Desktop.
publisher sample
package pubsub
import (
"context"
"cloud.google.com/go/pubsub"
"google.golang.org/api/option"
)
type Publisher struct {
client *pubsub.Client
}
func NewPublisher(ctx context.Context, projectID string, opts ...option.ClientOption) (*Publisher, error) {
client, err := pubsub.NewClient(ctx, projectID, opts...)
if err != nil {
return nil, err
}
return &Publisher{
client: client,
}, nil
}
func (p *Publisher) Publish(ctx context.Context, topicID string, msg string) error {
topic := p.client.Topic(topicID)
_, err := topic.Publish(ctx, &pubsub.Message{
Data: []byte(msg),
}).Get(ctx)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment