go golang connect to mssql tutorial basic

• 841
• 0
• 2018-05-17

golang go connect to mssql

I describe my process of finding way to use go connect to mssql here.

First I find this turorial video, and I learn the basic concept of using go conncect to database

Then I find the database/sql doc of go here:

https://golang.org/pkg/database/sql/

the key concept is:

Package sql provides a generic interface around SQL (or SQL-like) databases.

The sql package must be used in conjunction with a database driver.
See https://golang.org/s/sqldrivers for a list of drivers.

That is if you want to connect to DB by go,

you have to use 2 things:

1.the sql interface, that is the database/sql  package

2.the driver, list in https://github.com/golang/go/wiki/SQLDrivers

in the list, I choose go-mssqldb as example

So, you have to import these 2 package first, that is

import (
"database/sql"
_ "github.com/denisenkom/go-mssqldb"
}

After knowing above, we can directly see the executable example,

Here is example of  go-mssqldb

https://github.com/denisenkom/go-mssqldb/tree/master/examples

and I think the most valuable example is tsql example:

https://github.com/denisenkom/go-mssqldb/blob/master/examples/tsql/tsql.go

if you want to let this example work,

you just need to configue your connection string at the top,

that is modify the example connection flag to

	var (
)