Get started with Google Go in Azure

Microsoft gives Go developers a set of Azure SDKs for Visual Studio, Azure Stack, and Azure itself

For a language that’s designed for systems-level programming, the Google Go language (Golang) is surprisingly popular. Designed by many of the original C designers, it’s gained a reputation as a tool for developing low-level systems, including many used on Azure. If you’ve run a Docker container, fired up any of Azure’s Kubernetes services, or used the Open Service Broker, you’re using code that’s written in Go.

While Go development began at Google, it’s now an open source project, with a widely distributed development team. Recent hires into Azure’s cloud developer advocacy team have brought several key players into Microsoft, where they continue to work on Go and associated projects. Microsoft is also working with other companies that use Go, like Hashicorp, with its Terraform multicloud infrastructure management tool that helps manage Azure container deployments.

Like C, Go is a low-level language. Unlike C, it’s designed to be safe, with a relatively minimal feature set that makes it easy to learn. Many of its features work well with cloud-native development models, especially its support for asynchronous operations between processes using channels to handle interprocess messaging.

Because Go lets channels to be used by more than two processes, you can quickly construct complex concurrent architectures that let Go applications support many familiar microservices patterns. Processes are implemented as goroutines, which are easy to launch; a big Go application could have hundreds or even thousands of goroutines running at any time.

With that combination of features, it’s no surprise that Microsoft recently released a Go SDK for Azure, with tools for linking Go code to core Azure services and for using Go to manage Azure resources. Available from a Github repository, it’s easy to add it to your development environment—especially if you’re using Visual Studio Code’s popular Go extension. All you need is the Go compiler and a command line.

Getting started with Go in Azure

One excellent option is the freely available Go extension for Microsoft’s Visual Studio Code. With support for a built-in terminal that gives access to both the Windows command line and the Windows Subsystem for Linux (WSL) Bash prompt, you can work with Go code in the editor, using the terminal to test your code. I got the combination up and running in only a few minutes; once I’d run the installers, all I needed to do was create a directory for my Go source code. A quick “Hello World” later, I had Go up and running on my development PC.

Installing the Azure SDK for Go is easy enough, using Go’s own go gettools. All you need is the URL of the SDK’s Github repo; Go will do the rest. That includes installing any dependencies you might need. There’s a separate SDK for Azure storage, so if you’re planning on using Go with Azure Blobs, you need to install that as well. Once it’s all in place, you’ll need to add the modules needed to work with Azure services, including tools for handling service authentication. Microsoft recommends also installing the Azure CLI, which can run locally as a Docker image or via the Azure Cloud Shell. If you’re using Visual Studio Code, you access the Azure CLI through its Azure Tools extension.

Using the Azure SDK for Go

Once you’ve installed and configured it, you can start to write Go code to manage your Azure services. One word of warning: The first piece of sample code in Microsoft’s documentation requires you to add authentication data to a struct in your source code and then compiles in this information. That’s a bad idea; if you’re building new services, it’s probably sensible to store this information outside your source code, especially if you’re using GitHub or similar tool for source control.

In practice, you should use environment variables in the Azure CLI to hold the information needed to provision and manage Azure resources. Your Go code can then query these, making it portable across instances and admin accounts. There’s a larger set of samples on Github that takes this approach, so once you’ve gone beyond the basics, you can use the authentication features from these samples as a guide to implementing more-secure code.

Microsoft’s Azure SDK is comprehensive, with tools for working with its ARM resource management service, for integrating Go with Azure’s prepackaged machine learning services, for monitoring service consumption, and for handling storage and analytics. And if the service you want to use with Go isn’t there yet, it’ll likely be added soon.

In practice, most of your Go code is going to handle systems-management tasks, probably across multiple Azure accounts, tenants, and regions, all controlled by scripts. You’ll write code that takes input from external sources, like the command line or from your management environment, especially if you’re calling Go applications from inside kubelets or from the Azure CLI. Go’s heritage as a Unix systems programming language makes this easy, helping you use it as a basis for your own automated cloud-management tools.

Logging and debugging

With your code running controlled by scripts or as part of service management framework, triggered automatically, you also need to include logging statements. Go includes its own log package, using print helper statements. These functions implement common logging actions, including panics and fatal errors. A fatal error will log your error message and then automatically terminate the program. Log entries are automatically time-stamped, and you can use a constant block to define the prefix text you want to use, right down to the microsecond. Multiple goroutines can share a single log file, so you can log an entire process.

Microsoft also provides tools for integrating Go code with its Application Insights telemetry, which brings logging into the Azure Portal. Using Application Insights, you can bring monitoring information from all your code into one place, which can help debug interactions among your management applications, your code, and Azure resources.

Go skills are portable across Windows and Unix, making them ideal for Microsoft’s increasingly hybrid Azure. By supporting the language used to build many of its newer open source platform tools, there’s also scope for improved platform management tools, using Go to link the Azure Portal to Kubernetes or to Docker at a much deeper level.

Azure’s support for Go is also available for Azure Stack. Code written to manage public cloud services will run locally, supporting automation of hybrid cloud infrastructures. Code that starts up local containers or VM instances can then move workloads to the public cloud when additional resources are required, extending on-premises operations, while keeping costs to a minimum.