简体   繁体   中英

How to setup project to work with odbc and mssql drivers?

I'm trying to use the odbc driver and I'm getting errors:

.\main.go:5: imported and not used: "code.google.com/p/odbc/api"  
.\main.go:72: undefined: Driver  
.\main.go:76: undefined: Driver

I tried to work with another driver: go get github.com/denisenkom/go-mssqldb but ran unto the same type of problem.
That's why I suspect I didn't set up the environment right but unable to find the issue.


My environment:

set GOARCH=amd64
set GOBIN=
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Go\Projects 
set GORACE=
set GOROOT=C:\Go 
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set TERM=dumb
set CC=gcc
set GOGCCFLAGS=-g -O2 -m64 -mthreads
set CXX=g++
set CGO_ENABLED=1

I installed odbc using go get code.google.com/p/odbc
Ran test and it finished fine:
C:\\Go\\Projects\\src\\code.google.com\\p\\odbc>go test -mssrv=.\\sqlexp -v -run=MS ... --- PASS: TestMSSQLUTF16ToUTF8 (0.00 seconds) === RUN TestMSSQLExecStoredProcedure --- PASS: TestMSSQLExecStoredProcedure (0.01 seconds) PASS ok code.google.com/p/odbc 0.574s

My Code: (copied from mssql_test.go):

package main  
import (  
    "code.google.com/p/odbc/api"  
    "database/sql"       
    "flag"  
    "fmt"  
    "os"  
    "runtime"      
    "testing"  
    "time"  
)  

func mssqlConnect() (db *sql.DB, stmtCount int, err error) {  
    .........  
    return db, db.Driver().(*Driver).Stats.StmtCount, nil  
}  
func closeDB(t *testing.T, db *sql.DB, shouldStmtCount, ignoreIfStmtCount int) {  
    s := db.Driver().(*Driver).Stats  
    ......  
.\main.go:5: imported and not used: "code.google.com/p/odbc/api"  
.\main.go:72: undefined: Driver  
.\main.go:76: undefined: Driver

Delete import

    "code.google.com/p/odbc/api"

Add import

    "code.google.com/p/odbc" 

Replace *Driver with *odbc.Driver . For example,

    return db, db.Driver().(*odbc.Driver).Stats.StmtCount, nil

and

    s := db.Driver().(*odbc.Driver).Stats

./main.go:5: imported and not used: "github.com/denisenkom/go-mssqldb" as mssql

Change import to

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

For example,

package main

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

func main() {
    conn, err := sql.Open("mssql", makeConnStr())
}

The Go Programming Language Specification

Import declarations

To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name:

 import _ "lib/math" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM