Although I love attending the SQLBits SQL Server conference, I always come away with a list of weekend projects and new things to learn. One thing I noticed in almost every session I attended was the demos were mostly in Visual Studio Code – which I hadn’t seen before, and listening to the talks on SQL Server on Linux, had me thinking do I need to think about becoming trilingual. So for this weekend’s project, I will be setting up Golang and SQL Server.
- Quick Go Setup Guide on Mac OS X. Visual Studio Code is one of the top options for Golang IDEs. Give it a try! Install Visual Studio Code.
- Visual Studio!= Visual Studio Code, but VsVim for Visual Studio is excellent, and this is from a daily/advanced Vim user. Tshannon on Mar 3, 2016 Same for me, which is why nvim is so exciting.
This got me thinking that I should check out its big brother; Visual Studio. While it is pretty amazing, the lack of first class or even a high quality extension for go leaves me going back to VSC. Any chance this situation will change? I started on a Visual Studio integration for Go Showing 1-12 of 12 messages. I started on a Visual Studio integration for Go: Sam Harwell. Possible, I favor the Visual Studio look-and-feel, so a great reference for how the integration might behave in an ideal end is the C# editor.
Right now I spend most of my time in Management Studio writing TSQL. And I use PowerShell whenever I need to do something on more than one machine at a time. But now Microsoft is embracing open source should I be thinking the same and learn a new language which isn’t so Microsoft-centric.
After talking to some experts, I narrowed the choice down to two; Python and Go (also referred to as Golang). I picked Golang as it’s relatively new (open sourced in 2009 but for a language is leading-edge, whereas Python dates back to the late 1980s); nothing more complicated than that as this project is just for fun!
Let’s install Golang
Head over to https://golang.org/dl/ and download the MSI file. I selected Microsoft Windows as I will be performing this demo on my Windows 10 laptop. Open the MSI file and follow the prompts to install Go. By default, the installer puts the Go distribution in c:Go.
One of the requirements of getting your Go environment up and running is to have Git working from the command prompt. If you haven’t already installed Git on your machine, download the tools from https://git-for-windows.github.io/.
I restarted my machine at this point, so the changes to the PATH environment variable took effect.
![Golang Golang](/uploads/1/2/6/4/126443979/889048524.png)
Golang Mac Os
To test the installs were successful and to check you are ready to move on, open a command prompt and type go version and git version
Setting up the Golang environment with Visual Studio Code
Before we install Visual Studio Code we need to create a new system variable GOPATH.
To navigate to environment variables – click in the “Cortana Ask Me Anything” windows on the task bar and type “Edit the system environment variables” and select that item. Then click the Environment Variable button and then the New button.
Click on Browse Directory button and select a folder which will be used to save your programs.
Next, we need to download Visual Studio Code from https://code.visualstudio.com. Installation is straightforward just double-click the EXE file and select all the defaults.
Start Visual Studio Code and bring up the Extensions view by clicking on the Extensions icon in the Activity Bar, or use shortcut Ctrl+Shift+X. Type Go and click on the Install button, and after a successful install, you’ll see a Reload button which will prompt you to restart Visual Studio Code to enable the new extension.
Our first Golang program – “Hello, World!”
Before we go any further, let’s test our environment and write our first Golang program. Press Ctrl+K Ctrl-O to open a new folder. Navigate to the folder we created earlier for the GOPATH variable, and click on “Select Folder”.
Then click on the New File icon which will be next to the folder name you just opened, and type in a filename, for example, “helloworld.go”.
Messages for missing packages will appear at the top of Visual Studio Code, just click on Install All.
Next cut & paste the code below into the editor and save the file.
To run our newly created program, switch to the Terminal window in Visual Studio Code and type go run .helloword.go
Golang and SQL Server
Before we can connect to SQL Server using Golang we need a Microsoft SQL Server specific driver. Run the following command in a command prompt to install the driver.
I used the code below to connect to the SQL Server instance I created in my last post (Installing SQL Server 2017 in a Linux container using Docker on Windows 10).
In the connection string, the user is “sa” and the password is “SA_PASSWORD=yourStrong(!)Password.”
I’m connecting to a default instance, but if you are connecting to a named instance, you will need to escape the with localhost<instance name>.
Golang For Mac
Get Started with Golang and SQL Server in Visual Studio Code