This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.
Go is a programming language that is created by Google. It is sometimes referred to as 'Golang' and it's completely open-source. It is statically typed and compiled and in that sense, it is kind of like C# and C. Go is very popular and it is used in big implementations, like in Docker and in parts of Netflix.
And now, just like with almost any programming language, you can use Go in Azure! You can do that with the Azure SDKs for Go. There are several SDKs:
- The Core Azure SDK for Go
- The Blob Storage SDK for Go
- The File Storage SDK for Go
- The Storage Queue SDK for Go
- The Event Hub SDK for Go
- The Service Bus SDK for Go
- The Application Insights SDK for Go
Manage Azure Blobs with Go
Let's build our first app in Go and use the Azure Blob Storage SDK. We'll build something simple that can interact with Azure Blob Storage. To get started, we'll first do some initial setup of things.
Initial setup
First things first:
- If you haven't already, install Go 1.8 or later
- Create an Azure Storage account. We'll use this in the application. From the Azure portal, you can create a general Storage Account by leaving things to their default settings as they are in the image below:
(Creating a general-purpose Azure Storage Account in the Azure portal)
- Next, go to the Azure Storage Account and click on the Access keys menu. Now copy the Storage account name and the Key (either Key1 or Key2)
(Azure Storage Access keys in the Azure portal)
- In the Go application, we'll use the Storage account name and the Access key from environment variables. In Windows, you can set these variables from the command window, like in the image below. If you are running Linux, you can see the command here.
(Setting Azure Storage credentials as environment variables)
Creating the Go application
Now that we have everything set up, we can create our Go application. If you want, you can use Go with VSCode. Just install this VSCode extension to make it work and get features like IntelliSense and debugging.
- Let's start at the beginning. First, we create a Go file that will contain our code. Just create a file that has the .go extension, like AzureBlobTest.go. VSCode even lets you save files as Go files.
- Now, in the AzureBlobTest.go file, write:
package main import ( "bufio" "bytes" "context" "fmt" "io/ioutil" "log" "math/rand" "net/url" "os" "strconv" "time" "github.com/Azure/azure-storage-blob-go/azblob" )
If you are using VSCode, you can don't have to type all of the things in the import statement, except for the Azure Blob Storage SDK one. VSCode will automatically detect which packages you need and put them in the import statement. The github.com/Azure/azure-storage-blob-go/azblob is the Azure Storage Blobs SDK.
- To make sure that the Azure Blob Storage SDK can be found and that the application always imports it, I will also create a go.mod file. In here, I write:
module main require github.com/Azure/azure-storage-blob-go v0.0.0-20181022225951-5152f14ace1c
This makes sure that the application actually downloads the SDK onto the machine. There are other methods to do this as well, but this one works for me.
- Next, we create a new function, like this:
- And in the main function, we'll start creating the code that talks to blob storage. I'll start with getting the Azure Storage account and Key from the environment variables
- Next, we use the credentials to create an Azure Storage pipeline and create a new Azure Blob Container. For the container name, we use a random string so that we minimize the chance that the container already exists. I don't show the code for getting the random string here. Don't worry, that is included in the finalized code sample. You can find a link to that in the conclusion of this post
- Now that we have a container, we can put blobs in it. The code below creates a file and uploads it as a Blob to the container
- And next, we loop through all of the files in the container and print them out on the screen
- And the final piece of code waits for the user to press ENTER and cleans everything up
That's it. Now we can test it out. You can run the code from VSCode, but also from the command line. To run it, you use:
And the output looks like this:
(Running the Go application)
If you don't press ENTER, you can now see the results of the application in Azure.
Go to the Azure portal and navigate to the Azure Storage Account
- Click on Blobs
- Now you'll see the container. Click on it and click on the blob within it. When you now click Edit Blob, you can see the content of the Blob.
(Azure Storage Blob in the Azure portal)
- You can now go back to the command prompt and press ENTER. This will delete the Azure Storage container and the blob in it.
Conclusion and where to find the source code
That's it! As you can see, it is relatively easy to use Azure with Go. You can really use almost any language to work with Azure and to create apps for Azure, now including Go. Besides working with Azure services, you can also create Go applications that run in Azure. You can, for instance, compile a Go application by executing the Go build filename.go command to get an executable file that contains everything it needs to run. And you can deploy that executable to run in a container, in App Service, in Azure Service Fabric or wherever you like. And you can find the complete source code of the application that we've built, in this GitHub repository.
Feel free to follow me on Twitter for daily software development updates.