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
- Download and install the latest 64-bit Go MSI distributable (https://golang.org/dl/)
- 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
. - 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
- Install Visual Studio Code https://code.visualstudio.com/
- 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.
- 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 typego 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.
- 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. - Next, press and create a directory called Hello in the go-work directory.
- Next, make sure you are in the hello directory by issue the
cd hello
command. - Next, click on File->New File and enter the following into the file:
- Now, click on File->Save As and save the file as Main.go into the c:\go-work\hello directory.
- 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?
- Installed and setup the AMD64 bit Go Lang Environment on Windows
- Configured our first Go Lang Workspace and ensure environment variables are properly setup
- Installed Visual Studio Code
- Installed and configured Go Lang extension for Visual Studio Code
- Verified that the Go Lang extension for Visual Studio Code is properly installed
- 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.
- 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: - Next, create a directory called VMTest in the c:\go-work directory
- 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.
- Open a command prompt and change into the c:\go-work\VMTest directory
- 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:
- Copy the structure from the
{}
(brackets) into a file called quickstart.auth and save this file into the VMTest directory. - 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.
- 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: - 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: - 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
- Log into the Azure Portal by navigating to https://portal.azure.com.
- Click on the Cloud Shell icon
- In the PowerShell window run the following commands:
az group delete -n GoVMQuickstart
The PowerShell will return the following: - 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: - At this point, we have cleaned up the resources for this sample.
What did we accomplish in phase 2?
- Installation of the Azure Go SDK
- Create our directory structure needed for the sample
- Created a service principal that allows our Go Lang app to authenticate with Azure
- Configured environment variables for the sample to leverage for the service principal
- Downloaded and installed the quickstart sample
- Executed and verified that the sample works
- 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
https://golang.org/
Help Installing the Go Lang Extension
Building Go Apps for Azure with the Go SDK
Microsoft Documents for Go SDK for Azure
0 comments