{"id":2661,"date":"2023-05-17T07:49:17","date_gmt":"2023-05-17T14:49:17","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sdk\/?p=2661"},"modified":"2023-05-17T07:49:17","modified_gmt":"2023-05-17T14:49:17","slug":"announcing-the-stable-release-of-the-azure-queue-storage-client-library-for-go","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sdk\/announcing-the-stable-release-of-the-azure-queue-storage-client-library-for-go\/","title":{"rendered":"Announcing the stable release of the Azure Queue Storage client library for Go"},"content":{"rendered":"<p>The Azure SDK for Go team at Microsoft is excited to announce the stable release of the <a href=\"https:\/\/pkg.go.dev\/github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue\">Azure Queue Storage client library for Go<\/a>. <a href=\"https:\/\/azure.microsoft.com\/products\/storage\/queues\/\">Azure Queue Storage<\/a> is a message queueing service for large workloads. Queue Storage gives you asynchronous message queueing for communication between application components, whether they&#8217;re running in the cloud, on the desktop, on-premises, or on mobile devices.<\/p>\n<h2>Install the package<\/h2>\n<p>The Queue Storage client library is named <code>azqueue<\/code>. To install the latest version of <code>azqueue<\/code>, use the <code>go get<\/code> command. You can use the Azure Identity library to authenticate the client application.<\/p>\n<pre><code class=\"language-bash\">go get github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue\r\n\r\n# Optionally, if you also want to use Azure Identity for authentication\r\ngo get github.com\/Azure\/azure-sdk-for-go\/sdk\/azidentity<\/code><\/pre>\n<p>To get started, you&#8217;ll need:<\/p>\n<ul>\n<li>An Azure subscription.<\/li>\n<li>A working development environment for Go version 1.18 or above.<\/li>\n<\/ul>\n<h2>Create a client<\/h2>\n<p><code>ServiceClient<\/code> or <code>QueueClient<\/code> types expose methods that invoke service operations or queue and message operations. A client is shareable between multiple goroutines simultaneously and safely.<\/p>\n<p>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 <code>azqueue.QueueClient<\/code> type provide methods to manipulate queues and messages within a storage account. You must specify the storage account when you create <code>azqueue.QueueClient<\/code>.<\/p>\n<p>You can create a client using an Queue Storage connection string (obtained via the Azure portal) or with a <code>TokenCredential<\/code> type, such as <code>DefaultAzureCredential<\/code> from the <code>azidentity<\/code> library.<\/p>\n<blockquote><p>NOTE: We recommend you use <code>TokenCredential<\/code> types for authentication. Connection string-based authentication isn&#8217;t recommended.<\/p><\/blockquote>\n<h3>Use the <code>DefaultAzureCredential<\/code> token credential (recommended)<\/h3>\n<p><code>DefaultAzureCredential<\/code> 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 <a href=\"https:\/\/pkg.go.dev\/github.com\/Azure\/azure-sdk-for-go\/sdk\/azidentity#section-readme\"><code>azidentity<\/code> documentation<\/a>.<\/p>\n<pre><code class=\"language-go\">import (\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/azidentity\"\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue\"\r\n)\r\n\r\nfunc main() {\r\n    tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)\r\n    if err != nil {\r\n        \/\/TODO: handle error\r\n        panic(err)\r\n    }\r\n\r\n    client, err := azqueue.NewServiceClient(\"&lt;Azure Storage URL ex: https:\/\/my-queue.queue.core.windows.net\/&gt;\", tokenCredential, nil)\r\n    if err != nil {\r\n        \/\/TODO: handle error\r\n        panic(err)\r\n    }\r\n}<\/code><\/pre>\n<h3>Use Azure Queue Storage connection string<\/h3>\n<p>Queue Storage also supports authentication using a connection string, which you can get from the Azure portal.<\/p>\n<pre><code class=\"language-go\">package main\r\n\r\nimport (\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue\"\r\n)\r\n\r\nfunc main() {\r\n    serviceClient, err := azqueue.NewServiceClientFromConnectionString(\"&lt;Azure Storage Connection String&gt;\", nil)\r\n    if err != nil {\r\n        \/\/TODO: handle error\r\n        panic(err)\r\n    }\r\n}<\/code><\/pre>\n<h2>Queue operations<\/h2>\n<p>The <code>azqueue.QueueClient<\/code> type exposes queue operations that manipulate the lifecycle of the queue messages.<\/p>\n<h3>Create a queue<\/h3>\n<p><code>CreateQueue<\/code> 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 <code>ResourceExistsError<\/code>.<\/p>\n<pre><code class=\"language-go\">import (\r\n    \"context\"\r\n    \"fmt\"\r\n    \"log\"\r\n    \"os\"\r\n\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/azcore\/to\"\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/azidentity\"\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue\"\r\n)\r\n\r\nfunc handleError(err error) {\r\n    if err != nil {\r\n        log.Fatal(err.Error())\r\n    }\r\n}\r\n\r\nfunc main() {\r\n    accountName, ok := os.LookupEnv(\"AZURE_STORAGE_ACCOUNT_NAME\")\r\n    if !ok {\r\n        panic(\"AZURE_STORAGE_ACCOUNT_NAME could not be found\")\r\n    }\r\n    queueURL := fmt.Sprintf(\"https:\/\/%s.queue.core.windows.net\/queue1\", accountName)\r\n\r\n    cred, err := azidentity.NewDefaultAzureCredential(nil)\r\n    handleError(err)\r\n\r\n    client, err := azqueue.NewQueueClient(queueURL, cred, nil)\r\n    handleError(err)\r\n\r\n    resp, err := client.Create(context.TODO(), \"testqueue\", &amp;azqueue.CreateQueueOptions{\r\n        Metadata: map[string]*string{\"hello\": to.Ptr(\"world\")},\r\n    })\r\n    handleError(err)\r\n    fmt.Println(resp)\r\n}<\/code><\/pre>\n<h3>Delete a queue<\/h3>\n<p>Azure deletes the queue and any messages within it. If the queue is missing, the method call raises a <code>ResourceNotFoundError<\/code>.<\/p>\n<pre><code class=\"language-go\">package main\r\n\r\nimport (\r\n    \"context\"\r\n    \"fmt\"\r\n    \"log\"\r\n    \"os\"\r\n\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/azidentity\"\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue\"\r\n)\r\n\r\nfunc handleError(err error) {\r\n    if err != nil {\r\n        log.Fatal(err.Error())\r\n    }\r\n}\r\n\r\nfunc main() {\r\n    accountName, ok := os.LookupEnv(\"AZURE_STORAGE_ACCOUNT_NAME\")\r\n    if !ok {\r\n        panic(\"AZURE_STORAGE_ACCOUNT_NAME could not be found\")\r\n    }\r\n    queueURL := fmt.Sprintf(\"https:\/\/%s.queue.core.windows.net\/queue1\", accountName)\r\n\r\n    cred, err := azidentity.NewDefaultAzureCredential(nil)\r\n    handleError(err)\r\n\r\n    client, err := azqueue.NewQueueClient(queueURL, cred, nil)\r\n    handleError(err)\r\n\r\n    resp, err := client.Delete(context.TODO(), nil)\r\n    handleError(err)\r\n    fmt.Println(resp)\r\n}<\/code><\/pre>\n<h2>Message operations<\/h2>\n<p>The <code>azqueue.QueueClient<\/code> 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.<\/p>\n<h3>Send a message<\/h3>\n<p><code>EnqueueMessage<\/code> sends a message to the queue that you pass as a parameter.<\/p>\n<pre><code class=\"language-go\">package main\r\n\r\nimport (\r\n    \"context\"\r\n    \"fmt\"\r\n    \"log\"\r\n    \"os\"\r\n\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/azidentity\"\r\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue\"\r\n)\r\n\r\nfunc handleError(err error) {\r\n    if err != nil {\r\n        log.Fatal(err.Error())\r\n    }\r\n}\r\n\r\nfunc main() {\r\n    accountName, ok := os.LookupEnv(\"AZURE_STORAGE_ACCOUNT_NAME\")\r\n    if !ok {\r\n        panic(\"AZURE_STORAGE_ACCOUNT_NAME could not be found\")\r\n    }\r\n    serviceURL := fmt.Sprintf(\"https:\/\/%s.queue.core.windows.net\/\", accountName)\r\n\r\n    cred, err := azidentity.NewDefaultAzureCredential(nil)\r\n    handleError(err)\r\n\r\n    client, err := azqueue.NewServiceClient(serviceURL, cred, nil)\r\n    handleError(err)\r\n\r\n    resp, err := client.CreateQueue(context.TODO(), \"testqueue\", &amp;azqueue.CreateOptions{\r\n        Metadata: map[string]*string{\"hello\": to.Ptr(\"world\")},\r\n    })\r\n    handleError(err)\r\n    fmt.Println(resp)\r\n\r\n    opts := &amp;azqueue.EnqueueMessageOptions{TimeToLive: to.Ptr(int32(10))}\r\n    queueClient := client.NewQueueClient(\"testqueue\")\r\n    resp1, err := queueClient.EnqueueMessage(context.Background(), \"test content\", opts)\r\n    handleError(err)\r\n    fmt.Println(resp1)\r\n\r\n    resp2, err := queueClient.DequeueMessage(context.Background(), nil)\r\n    handleError(err)\r\n    \/\/ check message content\r\n    fmt.Println(resp2.Messages[0].MessageText)\r\n\r\n    \/\/ delete the queue\r\n    _, err = client.DeleteQueue(context.TODO(), \"testqueue\", nil)\r\n    handleError(err)\r\n    fmt.Println(resp)\r\n}<\/code><\/pre>\n<h2>Summary<\/h2>\n<p>The Queue Storage for Go library allows users to manipulate queues in Azure Storage. To learn more, see our <a href=\"https:\/\/pkg.go.dev\/github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue#section-readme\">documentation<\/a>. You can also find more examples either on <a href=\"https:\/\/pkg.go.dev\/github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azqueue#pkg-examples\">pkg.go.dev<\/a> or in our <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-go\/tree\/main\/sdk\/storage\/azqueue\">GitHub repository<\/a>.<\/p>\n<h2>Feedback<\/h2>\n<p>We&#8217;d love to hear about your experiences using the Azure SDK for Go. Send us your feedback on our <a href=\"https:\/\/gophers.slack.com\/archives\/CA7HK8EEP\">Slack Channel<\/a> or at the <a href=\"https:\/\/discordapp.com\/channels\/723347736853741589\/933781546815606885\">#golang-friends<\/a> channel on the Microsoft Open Source Discord Server.<\/p>\n<p>For feature requests, bug reports, or general support, <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-go\/issues\/new\/choose\">open an issue<\/a> in the Azure SDK for Go repository on GitHub. For more information on how we triage issues, see the <a href=\"https:\/\/devblogs.microsoft.com\/azure-sdk\/github-issue-support-process\/\">Azure SDK GitHub Issue Support Process<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Announcing the stable release of the Azure Queue Storage client library for Go<\/p>\n","protected":false},"author":119078,"featured_media":2669,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[906,810,811,907,24],"class_list":["post-2661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sdk","tag-azure-storage-queue","tag-go","tag-golang","tag-queues","tag-releases"],"acf":[],"blog_post_summary":"<p>Announcing the stable release of the Azure Queue Storage client library for Go<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/2661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/users\/119078"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/comments?post=2661"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/2661\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media\/2669"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media?parent=2661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/categories?post=2661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/tags?post=2661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}