The Go SDK is currently in experimental status. If you would like to provide feedback, please reach out to us with your suggestions and comments on our Discord.
Go - Queue.Enqueue()
Send messages to a queue.
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
queue, err := nitric.NewQueue("queue-name").Allow(nitric.QueueEnqueue, nitric.QueueDequeue)
if err != nil {
fmt.Println(err)
return
}
messages := []map[string]interface{}{
{
"message": "hello world",
},
}
failedMessages, err := queue.Enqueue(context.TODO(), messages)
if err != nil {
fmt.Println(err)
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Parameters
- Name
ctx
- Required
- Required
- Type
- context
- Description
The context of the call, used for tracing.
- Name
messages
- Required
- Required
- Type
- []map[string]interface{}
- Description
A message or an array of messages to send to the queue.
Examples
Send messages to a queue
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
queue, err := nitric.NewQueue("queue-name").Allow(nitric.QueueEnqueue)
if err != nil {
fmt.Println(err)
return
}
messages := []map[string]interface{}{
{
"type": "email",
"to": "hello@example.com",
"subject": "Notification",
"message": "A notification from Nitric",
},
{
"type": "SMS",
"to": "+17200000000",
"message": "A text message from Nitric",
},
}
failedMessages, err := queue.Enqueue(context.TODO(), messages)
if err != nil {
fmt.Println(err)
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Dealing with failures
In rare cases when enqueue messages some might fail to be sent. The response from Enqueue()
will include an array of any messages that failed to send. You can process this array to retry or log the error.
failedMessages, err := queue.Enqueue(context.TODO(), messages)
if err != nil {
fmt.Println(err)
return
}
for _, message := range failedMessages {
fmt.Println(message.Message) // The message that failed to enqueue
fmt.Println(message.Reason) // The reason the message failed to enqueue
}