This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Database/sql" "Flag" "FMT" "Log") Import (_ "Github.com/mattn/go-adodb") Var (local Boolremoteip Stringremoteds string) Func init () {flag. Boolvar (&local, "local", True, "set window Connect.") Flag. Stringvar (&remoteip, "Remoteip", "192.168.1.104", "Set up remote MSSQL of IP.") Flag. Stringvar (&remoteds, "Remoteds", "MSSQLSERVER", "Set up remote mssql of DataSource.")} Type Mssql struct {*sql. Dbdatasource stringdatabase stringwindows boolsa *sa}type SA struct {user stringpasswd stringport Int}fun C Newmssql () *mssql {mssql: = new (MSSQL) DataS: = "localhost" if!local {DataS = fmt. Sprintf ("%s\\%s", Remoteip, remoteds)}mssql = &mssql{//If the database is the default instance (MSSQLSERVER), the IP is used directly, and the named instance needs to be specified. DataSource: "192.168.1.104\\mssqlserver", Datasource:datas,database: "Iman",//Windows:true for Windows authentication, False SA account and password must be set Windows:local,sa: &sa{user: "sa", passwd: "123456", Port:1433,},}return Mssql}func (M *mssql) Open () ER Ror {config: = fmt. Sprintf ("Provider=sqloledb;initial catalog=%s;data source=%s ", M.database, M.datasource) if m.windows {config = fmt. Sprintf ("%s;integrated security=sspi", config)} else {//SQL 2000 has a different port notation than SQL 2005, separated by a comma after data Source. Config = fmt. Sprintf ("%s,%d;user id=%s;password=%s", config, M.sa.port, M.sa.user, m.sa.passwd)}var err Errorm. DB, err = sql. Open ("ADODB", config) fmt. Println (config) return Err}func (M *mssql) Select () {rows, err: = M.query ("Select ID, name from users") if err! = Nil {FMT.P rintf ("Select query err:%s\n", err)}for rows. Next () {var id, name Stringrows. Scan (&id, &name) fmt. PRINTF ("id =%s, name =%s\n", ID, name)}}func main () {flag. Parse () MSSQL: = Newmssql () Err: = MSSQL. Open () checkerror (ERR) MSSQL. Select ()}func checkerror (err error) {if err! = Nil {log. Fatal (ERR)}}