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.

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.

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

Golang and SQL Server - check golang and Git are installed

 

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.

Golang and SQL Server - set GOPATH

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.

Golang and SQL Server - install lukehoban Go 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”.

Golang and SQL Server - first golang program hello world

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.

package main

import "fmt"

func main() {
  fmt.Println("Hello, World!")
}

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 - running are first golang program

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.

go get github.com/denisenkom/go-mssqldb
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>.

 

package main

import (
  "database/sql"
  "fmt"
  "log"

  _ "github.com/denisenkom/go-mssqldb"
)

func main() {
  condb, errdb := sql.Open("mssql", "server=localhost;user id=sa;password=SA_PASSWORD=yourStrong(!)Password;")
  if errdb != nil {
    fmt.Println(" Error open db:", errdb.Error())
  }

  var (
    sqlversion string
  )

  rows, err := condb.Query("select @@version")
  if err != nil {
    log.Fatal(err)
  }
  for rows.Next() {
    err := rows.Scan(&sqlversion)
    if err != nil {
      log.Fatal(err)
    }
    log.Println(sqlversion)
  }
  defer condb.Close()
}

Golang and SQL Server - connecting to SQL Server in Docker

 

Get Started with Golang and SQL Server in Visual Studio Code

One thought on “Get Started with Golang and SQL Server in Visual Studio Code

Comments are closed.

Follow on Feedly
%d bloggers like this: