Why would 'Open connection failed:sql: unknown driver "mssql" (forgotten import?)' happen the first time on a go build?

huangapple go评论81阅读模式
英文:

Why would 'Open connection failed:sql: unknown driver "mssql" (forgotten import?)' happen the first time on a go build?

问题

第一次运行https://github.com/denisenkom/go-mssqldb/blob/master/examples/simple.go时,我遇到了错误'Open connection failed:sql: unknown driver "mssql" (forgotten import?)'。

我通过将
import _ "github.com/denisenkom/go-mssqldb"
更改为
import "github.com/denisenkom/go-mssqldb"
来解决了这个问题。

这样做会出现不同的错误'imported and not used: "github.com/denisenkom/go-mssqldb" as mssql'。

然而...在改回 import _ "github.com/denisenkom/go-mssqldb" 并重新构建后,第一个错误消失了。

我还不得不将主函数的名称从'simplemain'更改为'main',才能编译成功。

为什么第一次构建时会出现错误'Open connection failed:sql: unknown driver "mssql" (forgotten import?)'?

英文:

The first time I ran https://github.com/denisenkom/go-mssqldb/blob/master/examples/simple.go I got the error 'Open connection failed:sql: unknown driver "mssql" (forgotten import?)'

I resolved this by changing
import _ "github.com/denisenkom/go-mssqldb"
to
import "github.com/denisenkom/go-mssqldb"

Which gave a different error of 'imported and not used: "github.com/denisenkom/go-mssqldb" as mssql'.

However ... after changing back to import _ "github.com/denisenkom/go-mssqldb" and building again the first error went away.

I also had to change the main function name from 'simplemain' to 'main' for some reason before it would compile.

Why would the error 'Open connection failed:sql: unknown driver "mssql" (forgotten import?)' happen on the first build?

答案1

得分: 3

预期的使用方式似乎是:

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

其目的是运行包的init()函数:

func init() {
sql.Register("mssql", &MssqlDriver{})
}

很可能是某个拼写错误导致了错误。

英文:

Intended way of use seems to be

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

which purpose is to run package's init() functions

func init() {
sql.Register("mssql", &MssqlDriver{})
}

most likely it's a some typo cause the error.

huangapple
  • 本文由 发表于 2016年9月8日 11:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/39381968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定