Go - NewQueue()

Creates a new Queue to send and receive asynchronous tasks.

import (
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/queues"
)
func main() {
queue := nitric.NewQueue("queue-name").Allow(queues.QueueEnqueue, queues.QueueDequeue)
nitric.Run()
}

Parameters

  • Name
    name
    Required
    Required
    Type
    string
    Description

    The unique name of this Queue within the app. Subsequent calls to NewQueue with the same name will return the same object.

Access

All Nitric resources provide access permissions you can use to specify the level of access your code needs to the resource. See here for details about infrastructure security.

Available permissions:


nitric.QueueEnqueue

This permission allows your service to enqueue new messages.


nitric.QueueDequeue

This permission allows your service to dequeue messages and complete them.


Notes

In most instances, code should either enqueue or dequeue from a queue, usually not both.

Examples

Create a Queue

import (
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/queues"
)
func main() {
queue := nitric.NewQueue("queue-name").Allow(queues.QueueEnqueue, queues.QueueDequeue)
nitric.Run()
}

Enqueue messages

import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/queues"
)
func main() {
queue := nitric.NewQueue("queue-name").Allow(queues.QueueEnqueue, queues.QueueDequeue)
messages := []map[string]interface{}{
{
"message": "hello world",
},
}
failedMessages, err := queue.Enqueue(context.TODO(), messages)
if err != nil {
fmt.Println(err)
return
}
nitric.Run()
}

Dequeue messages

import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/queues"
)
func main() {
queue := nitric.NewQueue("queue-name").Allow(queues.QueueDequeue)
ctx := context.TODO()
// dequeue up to 10 messages
messages, err := queue.Dequeue(ctx, 10)
if err != nil {
return
}
for _, message := range messages {
fmt.Println("Message: ", message.Message())
message.Complete(ctx)
}
nitric.Run()
}
Last updated on Oct 15, 2024