Documentation
¶
Index ¶
- type Goop
- func (g *Goop) CreateClient() error
- func (g *Goop) CreateSubscription(topic *pubsub.Topic, subName string) (*pubsub.Subscription, error)
- func (g *Goop) CreateTopic(topicName string) (*pubsub.Topic, error)
- func (g *Goop) Publish(topicName, msg string) error
- func (g *Goop) PublishWithAttributes(topicName, msg string, attributes map[string]string) error
- func (g *Goop) PullMessages(subName string, messageCallback func(msg *pubsub.Message, g *Goop) error) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Goop ¶
type Goop struct {
Context context.Context
Project string
Opts []option.ClientOption
Client *pubsub.Client
}
Goop - Wrapper for Google Cloud Pub/Sub.
func (*Goop) CreateClient ¶
CreateClient - Create a new Pub/Sub client.
Return:
error - An error if it occurred.
Example ¶
ExampleGoop_CreateClient - Example usage of the CreateClient function.
ctx := context.Background()
// Create Pub/Sub wrapper.
g := &Goop{
Context: ctx,
Project: "project-id",
}
// Create the client.
if err := g.CreateClient(); err != nil {
log.Fatalf("\nFailed to create Pub/Sub client. Reason - %+v\n", err)
}
func (*Goop) CreateSubscription ¶
func (g *Goop) CreateSubscription(topic *pubsub.Topic, subName string) (*pubsub.Subscription, error)
CreateSubscription - Create a new Pub/Sub subscription if it does not already exist.
Params:
topic *pubsub.Topic - The topic to get messages from. subName string - The name of the subscription to use.
Return:
*pubsub.Topic - Pointer to a Pub/Sub subsription. error - An error if it occurred.
Example ¶
ExampleGoop_CreateSubscription - Example usage of the CreateSubscription function.
ctx := context.Background()
// Create Pub/Sub wrapper.
g := &Goop{
Context: ctx,
Project: "project-id",
}
// Create the client.
if err := g.CreateClient(); err != nil {
log.Fatalf("\nFailed to create Pub/Sub client. Reason - %+v\n", err)
}
// Create the topic.
topic, err := g.CreateTopic("my-awesome-topic")
if err != nil {
log.Fatalf("\nFailed to create Pub/Sub topic. Reason - %+v\n", err)
}
// Create the subscription.
_, subErr := g.CreateSubscription(topic, "my-awesome-subscription")
if subErr != nil {
log.Fatalf("\nFailed to create Pub/Sub subscription. Reason - %+v\n", err)
}
func (*Goop) CreateTopic ¶
CreateTopic - Create a new Pub/Sub topic if it does not already exist.
Params:
topicName string - The name of the topic to create.
Return:
*pubsub.Topic - Pointer to a Pub/Sub topic. error - An error if it occurred.
Example ¶
ExampleGoop_CreateTopic - Example usage of the CreateTopic function.
ctx := context.Background()
// Create Pub/Sub wrapper.
g := &Goop{
Context: ctx,
Project: "project-id",
}
// Create the client.
if err := g.CreateClient(); err != nil {
log.Fatalf("\nFailed to create Pub/Sub client. Reason - %+v\n", err)
}
// Create the topic.
topic, err := g.CreateTopic("my-awesome-topic")
if err != nil {
log.Fatalf("\nFailed to create Pub/Sub topic. Reason - %+v\n", err)
}
_ = topic // TODO: use the topic.
func (*Goop) Publish ¶
Publish - Publish a message to a Pub/Sub topic.
Params:
topicName string - The name of the topic to publish to. msg string - The message body to publish.
Return:
error - An error if it occurred.
Example ¶
ExampleGoop_Publish - Example usage of the Publish function.
ctx := context.Background()
// Create Pub/Sub wrapper.
g := &Goop{
Context: ctx,
Project: "project-id",
}
// Publish the message.
err := g.Publish("my-other-awesome-topic", "Hello world!")
if err != nil {
log.Fatalf("\nFailed to publish Pub/Sub messages. Reason - %+v\n", err)
}
func (*Goop) PublishWithAttributes ¶
PublishWithAttributes - Publish a message with attributes to a Pub/Sub topic.
Params:
topicName string - The name of the topic to publish to. msg string - The message body to publish. attributes map[string]string - Map of attributes (key/value) to publish along with the message body.
Return:
error - An error if it occurred.
Example ¶
ExampleGoop_PublishWithAttributes - Example usage of the PublishWithAttributes function.
ctx := context.Background()
// Create Pub/Sub wrapper.
g := &Goop{
Context: ctx,
Project: "project-id",
}
// Make some attributes.
messageAttributes := map[string]string{
"answer_to_life_universe_everything": "42",
}
// Publish the message with attributes.
err := g.PublishWithAttributes("my-other-awesome-topic", "Hello world!", messageAttributes)
if err != nil {
log.Fatalf("\nFailed to publish Pub/Sub messages. Reason - %+v\n", err)
}
func (*Goop) PullMessages ¶
func (g *Goop) PullMessages(subName string, messageCallback func(msg *pubsub.Message, g *Goop) error) error
PullMessages - Pull messages from a Pub/Sub subscription.
Params:
subName string - The name of the subscription to use. messageCallback func(msg *pubsub.Message, g *Goop) error - Callback function to fire for each message pulled from the topic.
Return:
error - An error if it occurred.
Example ¶
ExampleGoop_PullMessages - Example usage of the PullMessages function.
ctx := context.Background()
// Create Pub/Sub wrapper.
g := &Goop{
Context: ctx,
Project: "project-id",
}
// Callback function.
myAwesomeCallback := func(msg *pubsub.Message, pubSub *Goop) error {
fmt.Printf("Got messages %+v", msg)
return nil
}
// Pull the messages.
if err := g.PullMessages("my-awesome-subscription", myAwesomeCallback); err != nil {
log.Fatalf("\nFailed to pull Pub/Sub messages. Reason - %+v\n", err)
}