(GoLang) 报错: panic: sql: Register called twice for driver postgres

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

(GoLang) panic: sql: Register called twice for driver postgres

问题

我可能在这上面花了太多时间,所以我决定在这里尝试一下。
我在弄清楚为什么我的 Register 被调用两次时遇到了问题。
我能想到的最好的解释是,在 sql.Register()sqlx.Connect() 处都调用了一次。但是如果我移除 sql.Register(),那么就没有驱动程序了。

老实说,我对 Go 语言还是比较新的,希望能得到任何方向上的帮助。

以下是没有 sql.Register 的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "database/sql"
  5. "github.com/jmoiron/sqlx"
  6. )
  7. const (
  8. host = "localhost"
  9. port = 5432
  10. user = "postgres"
  11. password = "password"
  12. dbname = "sampledb"
  13. )
  14. /*-------------------------------------------*/
  15. /* Functions */
  16. /*-------------------------------------------*/
  17. // Error Checking Fn
  18. func CheckError(err error, str string) {
  19. if err != nil {
  20. fmt.Printf("Error @ : %s\n", str)
  21. panic(err)
  22. }
  23. }
  24. /*-------------------------------------------*/
  25. /* Main() */
  26. /*-------------------------------------------*/
  27. func main() {
  28. // Open DB Conn
  29. psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
  30. // sql.Register("postgres", &pq.Driver{})
  31. fmt.Printf(":: Drivers ::\n%s\n", sql.Drivers())
  32. db, err := sqlx.Connect("postgres", psqlconn)
  33. CheckError(err, "Main: sqlx.connect")
  34. defer db.Close()
  35. }

没有 sql.Register 的错误信息:

  1. $ go run name-generator.go
  2. :: Drivers ::
  3. []
  4. Error @ : Main: sqlx.connect
  5. panic: sql: unknown driver "postgres" (forgotten import?)
  6. goroutine 1 [running]:
  7. main.CheckError({0xc84320, 0xc000056680}, {0xc6073f, 0x5})
  8. C:/path/to/program.go:26 +0xa7 <--- Func CheckError(): panic(err)
  9. main.main()
  10. C:/path/to/program.go:40 +0x125 <--- Func Main(): CheckError()
  11. exit status 2

以下是有 sql.Register 的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "database/sql"
  5. "github.com/jmoiron/sqlx"
  6. "github.com/lib/pq"
  7. )
  8. const (
  9. host = "localhost"
  10. port = 5432
  11. user = "postgres"
  12. password = "password"
  13. dbname = "sampledb"
  14. )
  15. /*-------------------------------------------*/
  16. /* Functions */
  17. /*-------------------------------------------*/
  18. // Error Checking Fn
  19. func CheckError(err error, str string) {
  20. if err != nil {
  21. fmt.Printf("Error @ : %s\n", str)
  22. panic(err)
  23. }
  24. }
  25. /*-------------------------------------------*/
  26. /* Main() */
  27. /*-------------------------------------------*/
  28. func main() {
  29. // Open DB Conn
  30. psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
  31. sql.Register("postgres", &pq.Driver{})
  32. fmt.Printf(":: Drivers ::\n%s\n", sql.Drivers())
  33. db, err := sqlx.Connect("postgres", psqlconn)
  34. CheckError(err, "Main: sqlx.connect")
  35. defer db.Close()
  36. }

sql.Register 的错误信息:

  1. $ go run name-generator.go
  2. panic: sql: Register called twice for driver postgres
  3. goroutine 1 [running]:
  4. database/sql.Register({0xa98bc9, 0x8}, {0xae6680, 0xc8a950})
  5. C:/Program Files/Go/src/database/sql/sql.go:51 +0x13d
  6. main.main()
  7. C:/path/to/program.go:38 +0x11b
  8. exit status 2

其他资源:

  • 类似的问题,但不能解决我的问题 链接
  • SQLX 文档 链接
  • SQL 文档 链接
英文:

I have probably spent way to much time on this, so I decided to try here.
I'm having trouble figuring out why my Register is being called twice?
Best I can figure, it seems to be calling once at sql.Register() and again at sqlx.Connect(). But if I remove the sql.Register(), then theres no drivers.

Honestly, I am pretty new to GoLang, I'm hoping for any sort of direction here.

Code - w/o sql.Register

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;database/sql&quot;
  5. &quot;github.com/jmoiron/sqlx&quot;
  6. )
  7. const (
  8. host = &quot;localhost&quot;
  9. port = 5432
  10. user = &quot;postgres&quot;
  11. password = &quot;password&quot;
  12. dbname = &quot;sampledb&quot;
  13. )
  14. /*-------------------------------------------*\
  15. || Functions ||
  16. \*-------------------------------------------*/
  17. // Error Checking Fn
  18. func CheckError(err error, str string) {
  19. if err != nil {
  20. fmt.Printf(&quot;Error @ : %s\n&quot;, str)
  21. panic(err)
  22. }
  23. }
  24. /*-------------------------------------------*\
  25. || Main() ||
  26. \*-------------------------------------------*/
  27. func main() {
  28. // Open DB Conn
  29. psqlconn := fmt.Sprintf(&quot;host=%s port=%d user=%s password=%s dbname=%s sslmode=disable&quot;, host, port, user, password, dbname)
  30. // sql.Register(&quot;postgres&quot;, &amp;pq.Driver{})
  31. fmt.Printf(&quot;:: Drivers ::\n%s\n&quot;, sql.Drivers())
  32. db, err := sqlx.Connect(&quot;postgres&quot;, psqlconn)
  33. CheckError(err, &quot;Main: sqlx.connect&quot;)
  34. defer db.Close()
  35. }

Error - w/o sql.Register

  1. $ go run name-generator.go
  2. :: Drivers ::
  3. []
  4. Error @ : Main: sqlx.connect
  5. panic: sql: unknown driver &quot;postgres&quot; (forgotten import?)
  6. goroutine 1 [running]:
  7. main.CheckError({0xc84320, 0xc000056680}, {0xc6073f, 0x5})
  8. C:/path/to/program.go:26 +0xa7 &lt;--- Func CheckError(): panic(err)
  9. main.main()
  10. C:/path/to/program.go:40 +0x125 &lt;--- Func Main(): CheckError()
  11. exit status 2

Code - w/ sql.Register

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;database/sql&quot;
  5. &quot;github.com/jmoiron/sqlx&quot;
  6. &quot;github.com/lib/pq&quot;
  7. )
  8. const (
  9. host = &quot;localhost&quot;
  10. port = 5432
  11. user = &quot;postgres&quot;
  12. password = &quot;password&quot;
  13. dbname = &quot;sampledb&quot;
  14. )
  15. /*-------------------------------------------*\
  16. || Functions ||
  17. \*-------------------------------------------*/
  18. // Error Checking Fn
  19. func CheckError(err error, str string) {
  20. if err != nil {
  21. fmt.Printf(&quot;Error @ : %s\n&quot;, str)
  22. panic(err)
  23. }
  24. }
  25. /*-------------------------------------------*\
  26. || Main() ||
  27. \*-------------------------------------------*/
  28. func main() {
  29. // Open DB Conn
  30. psqlconn := fmt.Sprintf(&quot;host=%s port=%d user=%s password=%s dbname=%s sslmode=disable&quot;, host, port, user, password, dbname)
  31. sql.Register(&quot;postgres&quot;, &amp;pq.Driver{})
  32. fmt.Printf(&quot;:: Drivers ::\n%s\n&quot;, sql.Drivers())
  33. db, err := sqlx.Connect(&quot;postgres&quot;, psqlconn)
  34. CheckError(err, &quot;Main: sqlx.connect&quot;)
  35. defer db.Close()
  36. }

Error - w/ sql.Register

  1. $ go run name-generator.go
  2. panic: sql: Register called twice for driver postgres
  3. goroutine 1 [running]:
  4. database/sql.Register({0xa98bc9, 0x8}, {0xae6680, 0xc8a950})
  5. C:/Program Files/Go/src/database/sql/sql.go:51 +0x13d
  6. main.main()
  7. C:/path/to/program.go:38 +0x11b
  8. exit status 2

Additional Resources

  • Similar issue, but doesn't solve my problem Link
  • SQLX Documentation Link
  • SQL Documentation Link

答案1

得分: 2

github.com/lib/pq在一个init函数中注册了它的驱动

从应用程序中删除直接调用注册驱动的代码行:

  1. sql.Register("postgres", &pq.Driver{}) // <-- 删除这行

导入github.com/lib/pq以执行init()函数的副作用:

  1. package main
  2. import (
  3. "fmt"
  4. "database/sql"
  5. "github.com/jmoiron/sqlx"
  6. _ "github.com/lib/pq" // <-- 添加这行
  7. )
英文:

The package github.com/lib/pq registers it's driver in an init function.

Remove the direct call to register the driver from the application:

  1. sql.Register(&quot;postgres&quot;, &amp;pq.Driver{}) &lt;-- delete this line

Import github.com/lib/pq for the side effect of executing the init() function:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;database/sql&quot;
  5. &quot;github.com/jmoiron/sqlx&quot;
  6. _ &quot;github.com/lib/pq&quot; // &lt;-- add this line
  7. )

huangapple
  • 本文由 发表于 2022年1月22日 12:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/70810126.html
匿名

发表评论

匿名网友

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

确定