Created
December 24, 2019 02:22
-
-
Save sugamon/a0cd6be4d4774601ae08b5e25f262147 to your computer and use it in GitHub Desktop.
publisher sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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