Hi, Habr! I present to you the translation of the article
“Get started with Google Go in Azure” .
Microsoft provides Go developers with the Azure SDK for Visual Studio, Azure Stack and Azure directly.
For a system-level language, Google Go (
Golang ) is surprisingly popular. Many C developers took part in its development, perhaps that is why he won a reputation as a tool for developing low-level systems, including many used on Azure. If you run the Docker container, activate any Azure Kubernetes service, or use Open Service Broker, you use code written in Go.
Although Go began development at Google, it is now an open source project with a widespread development team. Recent Azure Cloud Developer team has brought several key players to Microsoft, where they continue to work on Go and related projects. Microsoft also works with other companies that use Go, such as Hashicorp, whose multi-tier Terraform tool helps manage Azure container deployments.
')
Like C, Go is a low-level language. But unlike C, it is designed with a relatively minimal set of features to be safe and allow for easy learning. Many of its functions work well with cloud models of development, especially with support for inter-process asynchronous operations using channels for processing interprocess messages.
Because Go allows channels to be used by more than two processes, you can quickly build complex parallel architectures that allow Go applications to support many familiar microservice templates. Processes are implemented as
goroutines , which run easily. A great go app can contain hundreds or even thousands of goroutines at any time.
With this combination of features, it's no surprise that Microsoft
recently released the Go SDK for Azure with tools to associate Go code with basic Azure services, as well as to manage Azure resources. Available from the Github repository, it is easy to add it to the development environment, especially if you use the popular Go extension for Visual Studio. All you need is a Go compiler and a command line.
Get started with Go in Azure
One of the excellent options to start with is the
Go open source extension
for Microsoft Visual Studio . Thanks to the support of the built-in terminal, which gives access to both the Windows command line and the
Windows Subsystem for Linux (WSL) , you can work with the Go code in the editor, using the terminal to check the code. Downloading and installing takes a few minutes. After running the installer, all you need to do is create a Go source directory. Then a quick “Hello World”, and you can start developing.
Installing the Azure SDK for Go is fairly straightforward with Go's own
go get tools. All you need is the URL of the SDK repository on Github; The next step is to install any dependencies that you may need. There is a separate SDK for working with Azure storage, so if you plan to use Go with Azure Blobs, you also need to install it. After everything is installed, you will need to add the modules you need to work with Azure services, including tools to manage authentication. Microsoft also recommends installing the Azure CLI, which can run locally as a Docker image or through the Azure Cloud shell. If you are using Visual Studio, you will access the Azure CLI interface through the Azure Tools extension.
Use the Azure SDK for Go
After you have installed and configured everything, you can start writing Go code to manage your Azure services.
Important warning : The first part of the sample code in the Microsoft documentation requires that you add authentication data to the structure in the source code and then compile it. This is a bad idea - if you are creating new services, it is probably wise to keep this information outside the source code, especially if you use GitHub or a similar version control tool.
In practice, you must use Azure CLI environment variables to store the information necessary to provision and manage Azure resources. The Go code can then request them, making it portable across all instances and administrator accounts. Github has a
wider set of patterns that use this approach, so as soon as you go beyond the basics, you can use the authentication features of these patterns as a guide to implementing more secure code.
The Microsoft Azure SDK is
comprehensive , having in its arsenal tools for working with the ARM resource management service, integrating Go with Azure machine learning services, monitoring service consumption, storage management and analytics. And if the service that you want to use with Go does not yet exist, it will most likely be added soon.
In practice, most of your Go code will handle system management tasks, possibly in several Azure accounts and regions, all of which are controlled by scripts. You will write code that takes input from external sources, such as the command line or from the management environment, especially if you call Go applications from within
Kubelet or from the Azure CLI. YOU can use it as a basis for your own automated cloud management tools.
Logging and debugging
When your code runs under script control or as part of a service management structure, it switches automatically — you need to turn on logging. Go includes its own logging package. It includes logging of common actions, including unrecoverable errors. A fatal error will result in the recording of an error message and the subsequent automatic shutdown of the program. Log entries are automatically tagged with a timestamp, and you can apply a sample down to a microsecond. Several goroutines can share one log file, so you can track the whole process.
Microsoft also provides tools for integrating Go code with
Application Insights . Using Application Insights, you can consolidate monitoring information from all of your code into one place, which can help debug interactions between your managed applications, your code, and Azure resources.
Go programming skills are portable between Windows and Unix, making them ideal for Microsoft's increasingly hybrid Azure. By supporting the language used to create many of our new open source tools, it is also possible to improve management tools using Go to link Azure Portal with Kubernetes or Docker to a much deeper level.
Azure Support for Go is also available for the Azure Stack. Code written to manage public cloud services will be run locally, supporting the automation of hybrid cloud infrastructures. Code running local containers or virtual machine instances can then move workloads to a public cloud when additional resources are required, expanding internal operations, while keeping costs to a minimum.