英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论