Using Go Lang with Azure

Developer Support

App Dev Manager Rick Caudle explores Go Lang to manage Azure VMs using Visual Studio Code, Go Lang, and the Azure Go Lang SDK.


If you have been struggling to get Go Lang properly working with Visual Studio Code or have been having challenges using the Azure Go SDK, my hope is that this post will help you overcome those challenges.

Preface:

In the past few years we have seen an increase in new programming languages. Go or Go Lang has been at the top of the list for these new programming languages. There are many reasons as to why these new programming languages are needed or are being preferred by many developers. I am not going to go into all the reasons as to why, but one reason at the core of this, is the fact that we have not seen much gain in raw processing power since 2004. Back in 2004 the Pentium 4 processor with a 3.0GHz clock was introduced. Today, a MacBook Pro with 6-core Intel i7 processor clocks in at 2.2GHz with Turbo Boost up to 4.1GHz. As you can glean from this the focus has shifted from single core processors to multicore processors.

What does this shift to multi-core / multiprocessor architecture mean to the developer? It’s simple, the paradigm shift is now pushing our developers to think more about how they can better leverage multicore/multiprocessor architecture. Many traditional languages are not very well suited for this, hence the introduction of language enhancements that allow for parallelism.

In this world of digital transformation, languages like Go Lang have a major impact on project success as it helps increase developer and software efficiency. Leveraging languages like Go Lang and combining this into your Cloud Development Strategy can make a lot of sense. If you are interested in reading more about the features/benefits of Go Lang, please review the references section below.

Today, we are going to take a very simple look at leveraging Go Lang to manage Azure VMs. For this effort we will be leveraging Visual Studio Code, Go Lang and Azure Go Lang SDK. My goal today is to get you up and running with Visual Studio Code, Go Lang and the Azure Go SDK.

Prerequisites

  1. Download and install the latest 64-bit Go MSI distributable (https://golang.org/dl/)
  2. Create a Go Workspace directory, in this example we will create a directory called c:\go-work. Open a Command Prompt and type the following: mkdir c:\go-work.
  3. Make sure your GOPATH environment variable is set to your Go Workspace directory created in step 2 more details on this topic can be found here
  4. Install Visual Studio Code https://code.visualstudio.com/
  5. Install the Go Lang Extension for Visual Studio Code by pressing Ctrl+Shift+X and searching for “Go”, then install the Go extension released by Microsoft.
  6. Verify the Go Lang Extension is installed and working by pressing  to open the Terminal Window. Next Type go version. Here is an example of what you should see You can also type go env to check your environment settings.

Phase 1: Let’s write our first Go Lang program

Goal: Verify we can successfully compile and execute a Go Lang Program.

  1. Open the Terminal window by pressing  and issue a CD command to change into our GO Workspace directory you created in step 2 of the prerequisites.
  2. Next, press  and create a directory called Hello in the go-work directory.
  3. Next, make sure you are in the hello directory by issue the cd hello command.
  4. Next, click on File->New File and enter the following into the file:
  5. Now, click on File->Save As and save the file as Main.go into the c:\go-work\hello directory.
  6. Now, we are ready to run the program by pressing  and typing go run main.go, your output should look like this:

What have we accomplished so far?

  1. Installed and setup the AMD64 bit Go Lang Environment on Windows
  2. Configured our first Go Lang Workspace and ensure environment variables are properly setup
  3. Installed Visual Studio Code
  4. Installed and configured Go Lang extension for Visual Studio Code
  5. Verified that the Go Lang extension for Visual Studio Code is properly installed
  6. Wrote and tested our first Go Lang application.

Next, we are going to look at leveraging the Go Lang Azure SDK to perform some very simple tasks in Azure.

Phase 2: Let’s dive into setting up and running the Go Azure SDK VM Deployment Quick Start example.

  1. Install the Azure Go SDK by pressing  and typing the following into the terminal window: go get -u -d github.com/Azure/azure-sdk-for-go/... the output window should look like this:
  2. Next, create a directory called VMTest in the c:\go-work directory
  3. Next, we need to create a service principal. In order to sign in non-interactively to Azure with an Application, we need a service principal. Service principals are part of role-based access control (RBAC), which creates a unique user identity. You will need to open the Azure CLI for this. You can install the Azure CLI from here if need.

    1. Open a command prompt and change into the c:\go-work\VMTest directory
    2. Login into the Azure Portal by navigating to portal.microsoft.com and click on the Cloud Shell icon and type the following into the PowerShell Window az ad sp create-for-rbac --sdk-auth The command should return a structure with the following details:
  4. Copy the structure from the {} (brackets) into a file called quickstart.auth and save this file into the VMTest directory.
  5. Next, set the AZURE_AUTH_LOCATION environment variable to be the full path to this file. In this example the full path would be C:\go-work\VMTest\quickstart.auth. You will need to restart Visual Studio Code so it will pick up the new environment variable.
  6. Next, we need to download the quickstart code sample. Press  to open the terminal window and run the following command: go get -u -d github.com/azure-samples/azure-sdk-for-go-samples/quickstarts/deploy-vm/... The output window should return with the following:
  7. Next, lets run the quickstart sample code. Press CTRL+` to open the terminal prompt in. Type the following command: cd c:/go-work/src/github.com/azure-samples/azure-sdk-for-go-samples/quickstarts/deploy-vm, the terminal should appear as follows:
  8. Next run the quickstart sample code. Enter the following into the terminal window: go run main.go, the terminal window should return with the following:

Cleaning up the resources

  1. Log into the Azure Portal by navigating to https://portal.azure.com.
  2. Click on the Cloud Shell icon
  3. In the PowerShell window run the following commands: az group delete -n GoVMQuickstart The PowerShell will return the following:
  4. Next we need to delete the service principal we created. Open the quickstart.auth file and find the value for the clientId and run the following command: az ad sp delete --id (clientid_guid) where clientid_guid is the valid you found for clientId in the quickstart.auth file. Once the command has completed, the window should appear as follows:
  5. At this point, we have cleaned up the resources for this sample.

What did we accomplish in phase 2?

  1. Installation of the Azure Go SDK
  2. Create our directory structure needed for the sample
  3. Created a service principal that allows our Go Lang app to authenticate with Azure
  4. Configured environment variables for the sample to leverage for the service principal
  5. Downloaded and installed the quickstart sample
  6. Executed and verified that the sample works
  7. Cleaned up the Deployment and removed the service principal

Closing:

Stay tuned for Part 2 of Using Go Lang with Azure Go Lang SDK. In Part 2 we will take a look at some of the capabilities of the Azure Go Lang SDK and drill into various capabilities.

Important Notes:

Debugging Go Lang with Delve requires you to install the 64-bit version of Go. Delve does not currently support debugging with the 386 version of Go Lang. It’s very important that you Go Lang Environment variables are properly set. All the AZ commands that were executed from the Portal can be executed from the Azure CLI. You may run into some challenges if you are using multi-factor authentication and is why this post leverages the Cloud Shell via the portal.

References

Azure CLI

https://golang.org/

Help Installing the Go Lang Extension

Azure Go Lang SDK

Building Go Apps for Azure with the Go SDK

Microsoft Documents for Go SDK for Azure

 

0 comments

Discussion is closed.

Feedback usabilla icon