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 - NewQueue()
Creates a new Queue to send and receive asynchronous tasks.
import (
"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
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
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 Access Control documentation.
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 (
"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
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Enqueue messages
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{}{}
failedMessages, err := queue.Enqueue(context.TODO(), messages)
if err != nil {
fmt.Println(err)
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Dequeue messages
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
queue, err := nitric.NewQueue("queue-name").Allow(nitric.QueueDequeue)
if err != nil {
fmt.Println(err)
return
}
tasks, err := queue.Dequeue(context.TODO(), 10)
if err != nil {
fmt.Println(err)
return
}
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}