go get github.com/denisenkom/go-mssqldb
The connection string can be specified in one of three formats:
ADO: key=value pairs separated by ;. Values may not contain ;, leading and trailing whitespace is ignored.
 Examples:
server=localhost\\SQLExpress;user id=sa;database=master;connection timeout=30server=localhost;user id=sa;database=master;connection timeout=30ODBC: Prefix with odbc, key=value pairs separated by ;. Allow ; by wrapping
values in {}. Examples:
odbc:server=localhost\\SQLExpress;user id=sa;database=master;connection timeout=30odbc:server=localhost;user id=sa;database=master;connection timeout=30odbc:server=localhost;user id=sa;password={foo;bar} // Value marked with {}, password is "foo;bar"odbc:server=localhost;user id=sa;password={foo{bar} // Value marked with {}, password is "foo{bar"odbc:server=localhost;user id=sa;password={foobar } // Value marked with {}, password is "foobar "odbc:server=localhost;user id=sa;password=foo{bar   // Literal {, password is "foo{bar"odbc:server=localhost;user id=sa;password=foo}bar   // Literal }, password is "foo}bar"odbc:server=localhost;user id=sa;password={foo{bar} // Literal {, password is "foo{bar"odbc:server=localhost;user id=sa;password={foo}}bar} // Escaped } with}}`, password is "foo}bar"URL: with sqlserver scheme. username and password appears before the host. Any instance appears as
the first segment in the path. All other options are query parameters. Examples:
sqlserver://username:password@host/instance?param1=value¶m2=valuesqlserver://username:password@host:port?param1=value¶m2=valuesqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30 // `SQLExpress instance.sqlserver://sa:mypass@localhost?database=master&connection+timeout=30     // username=sa, password=mypass.sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30" // port 1234 on localhost.sqlserver://sa:my%7Bpass@somehost?connection+timeout=30 // password is "my{pass"A string of this format can be constructed using the URL type in the net/url package.
  query := url.Values{}
  query.Add("connection timeout", fmt.Sprintf("%d", connectionTimeout))
  u := &url.URL{
      Scheme:   "sqlserver",
      User:     url.UserPassword(username, password),
      Host:     fmt.Sprintf("%s:%d", hostname, port),
      // Path:  instance, // if connecting to an instance instead of a port
      RawQuery: query.Encode(),
  }
  connectionString := u.String()
  db, err := sql.Open("sqlserver", connectionString)
  // or
  db, err := sql.Open("mssql", connectionString)
The sqlserver driver uses normal MS SQL Server syntax and expects parameters in
the sql query to be in the form of either @Name or @p1 to @pN (ordinal position).
db.QueryContext(ctx, `select * from t where ID = @ID;`, sql.Named("ID", 6))
For the mssql driver, the SQL statement text will be processed and literals will
be replaced by a parameter that matches one of the following:
where nnn represents an integer that specifies a 1-indexed positional parameter. Ex:
db.Query("SELECT * FROM t WHERE a = ?3, b = ?2, c = ?1", "x", "y", "z")
will expand to roughly
SELECT * FROM t WHERE a = 'z', b = 'y', c = 'x'
go test is used for testing. A running instance of MSSQL server is required.
Environment variables are used to pass login information.
Example:
env HOST=localhost SQLUSER=sa SQLPASSWORD=sa DATABASE=test go test