[英]How to setup project to work with odbc and mssql drivers?
I'm trying to use the odbc driver and I'm getting errors: 我正在尝试使用odbc驱动程序,但出现错误:
.\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. 我尝试使用其他驱动程序:
go get github.com/denisenkom/go-mssqldb
但遇到相同类型的问题。
That's why I suspect I didn't set up the environment right but unable to find the issue. 这就是为什么我怀疑我没有正确设置环境但是找不到问题的原因。
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
我使用
go get code.google.com/p/odbc
安装了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): 我的代码:( 从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
删除
import
"code.google.com/p/odbc/api"
Add import
添加
import
"code.google.com/p/odbc"
Replace *Driver
with *odbc.Driver
. 将
*Driver
替换为*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 import
更改为
_ "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
Go编程语言规范
To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name:
要仅出于副作用(初始化)导入软件包,请使用空白标识符作为显式软件包名称:
import _ "lib/math"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.