The Azure SDK for Go team at Microsoft is excited to announce the stable release of the Azure Queue Storage client library for Go. Azure Queue Storage is a message queueing service for large workloads. Queue Storage gives you asynchronous message queueing for communication between application components, whether they’re running in the cloud, on the desktop, on-premises, or on mobile devices.
Install the package
The Queue Storage client library is named azqueue
. To install the latest version of azqueue
, use the go get
command. You can use the Azure Identity library to authenticate the client application.
go get github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue
# Optionally, if you also want to use Azure Identity for authentication
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
To get started, you’ll need:
- An Azure subscription.
- A working development environment for Go version 1.18 or above.
Create a client
ServiceClient
or QueueClient
types expose methods that invoke service operations or queue and message operations. A client is shareable between multiple goroutines simultaneously and safely.
Queue Storage offers three types of resources, the storage account that has one or more queues and one or more messages in the queue. Instances of the azqueue.QueueClient
type provide methods to manipulate queues and messages within a storage account. You must specify the storage account when you create azqueue.QueueClient
.
You can create a client using an Queue Storage connection string (obtained via the Azure portal) or with a TokenCredential
type, such as DefaultAzureCredential
from the azidentity
library.
NOTE: We recommend you use
TokenCredential
types for authentication. Connection string-based authentication isn’t recommended.
Use the DefaultAzureCredential
token credential (recommended)
DefaultAzureCredential
combines several credential types into one easy-to-use type. It can authenticate using the Azure CLI, managed identities, and more. For more information, see the azidentity
documentation.
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
)
func main() {
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
//TODO: handle error
panic(err)
}
client, err := azqueue.NewServiceClient("<Azure Storage URL ex: https://my-queue.queue.core.windows.net/>", tokenCredential, nil)
if err != nil {
//TODO: handle error
panic(err)
}
}
Use Azure Queue Storage connection string
Queue Storage also supports authentication using a connection string, which you can get from the Azure portal.
package main
import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
)
func main() {
serviceClient, err := azqueue.NewServiceClientFromConnectionString("<Azure Storage Connection String>", nil)
if err != nil {
//TODO: handle error
panic(err)
}
}
Queue operations
The azqueue.QueueClient
type exposes queue operations that manipulate the lifecycle of the queue messages.
Create a queue
CreateQueue
is a method that creates a new queue under the specified account from the service client. This method returns a client for interacting with the newly created queue. If a queue with the same name already exists, the method call raises a ResourceExistsError
.
import (
"context"
"fmt"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
)
func handleError(err error) {
if err != nil {
log.Fatal(err.Error())
}
}
func main() {
accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
if !ok {
panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
}
queueURL := fmt.Sprintf("https://%s.queue.core.windows.net/queue1", accountName)
cred, err := azidentity.NewDefaultAzureCredential(nil)
handleError(err)
client, err := azqueue.NewQueueClient(queueURL, cred, nil)
handleError(err)
resp, err := client.Create(context.TODO(), "testqueue", &azqueue.CreateQueueOptions{
Metadata: map[string]*string{"hello": to.Ptr("world")},
})
handleError(err)
fmt.Println(resp)
}
Delete a queue
Azure deletes the queue and any messages within it. If the queue is missing, the method call raises a ResourceNotFoundError
.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
)
func handleError(err error) {
if err != nil {
log.Fatal(err.Error())
}
}
func main() {
accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
if !ok {
panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
}
queueURL := fmt.Sprintf("https://%s.queue.core.windows.net/queue1", accountName)
cred, err := azidentity.NewDefaultAzureCredential(nil)
handleError(err)
client, err := azqueue.NewQueueClient(queueURL, cred, nil)
handleError(err)
resp, err := client.Delete(context.TODO(), nil)
handleError(err)
fmt.Println(resp)
}
Message operations
The azqueue.QueueClient
type exposes these operations that interact with the messages that reside inside the Azure Storage queue. These operations require a valid Azure Storage queue name that you pass as a parameter.
Send a message
EnqueueMessage
sends a message to the queue that you pass as a parameter.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
)
func handleError(err error) {
if err != nil {
log.Fatal(err.Error())
}
}
func main() {
accountName, ok := os.LookupEnv("AZURE_STORAGE_ACCOUNT_NAME")
if !ok {
panic("AZURE_STORAGE_ACCOUNT_NAME could not be found")
}
serviceURL := fmt.Sprintf("https://%s.queue.core.windows.net/", accountName)
cred, err := azidentity.NewDefaultAzureCredential(nil)
handleError(err)
client, err := azqueue.NewServiceClient(serviceURL, cred, nil)
handleError(err)
resp, err := client.CreateQueue(context.TODO(), "testqueue", &azqueue.CreateOptions{
Metadata: map[string]*string{"hello": to.Ptr("world")},
})
handleError(err)
fmt.Println(resp)
opts := &azqueue.EnqueueMessageOptions{TimeToLive: to.Ptr(int32(10))}
queueClient := client.NewQueueClient("testqueue")
resp1, err := queueClient.EnqueueMessage(context.Background(), "test content", opts)
handleError(err)
fmt.Println(resp1)
resp2, err := queueClient.DequeueMessage(context.Background(), nil)
handleError(err)
// check message content
fmt.Println(resp2.Messages[0].MessageText)
// delete the queue
_, err = client.DeleteQueue(context.TODO(), "testqueue", nil)
handleError(err)
fmt.Println(resp)
}
Summary
The Queue Storage for Go library allows users to manipulate queues in Azure Storage. To learn more, see our documentation. You can also find more examples either on pkg.go.dev or in our GitHub repository.
Feedback
We’d love to hear about your experiences using the Azure SDK for Go. Send us your feedback on our Slack Channel or at the #golang-friends channel on the Microsoft Open Source Discord Server.
For feature requests, bug reports, or general support, open an issue in the Azure SDK for Go repository on GitHub. For more information on how we triage issues, see the Azure SDK GitHub Issue Support Process.
0 comments