Choose an SQL driver at runtime in Golang when drivers have the same name

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

Choose an SQL driver at runtime in Golang when drivers have the same name

问题

我想知道在Golang中是否有一种方法或投影模式,可以在运行时选择SQL驱动程序,当这两个驱动程序具有相同的名称时。我想通过使用环境变量在HTTP ClickHouse驱动程序(https://github.com/mailru/go-clickhouse)和本机TCP ClickHouse驱动程序(https://github.com/ClickHouse/clickhouse-go)之间进行切换。

通常情况下,这会导致“panic: sql: Register called twice for driver clickhouse”。有没有可能避免这种情况发生?

英文:

I'd like to know if there is an approach or projection pattern to be able to choose SQL driver at runtime in Golang when both of these drivers have the same name. I want to switch between HTTP ClickHouse driver (https://github.com/mailru/go-clickhouse) and native TCP ClickHouse driver (https://github.com/ClickHouse/clickhouse-go) using an environment variable.

import(
//HTTP driver
_ "github.com/mailru/go-clickhouse"
)

func getHttpCHConnection() (*sql.DB, error) {
   ...
   db, err := sql.Open("clickhouse", clkConnUrl)
import(
//Native driver
_ "github.com/ClickHouse/clickhouse-go"
)

func getNativeCHConnection() (*sql.DB, error) {
    ...
	db, err := sql.Open("clickhouse", clkConnUrl)

Normally, it causes "panic: sql: Register called twice for driver clickhouse". Is it possible to avoid that?

答案1

得分: 0

自从mailru/go-clickhouse的第2个版本开始,可以同时使用这两个驱动程序,作者已将驱动程序名称更改为chhttp:https://github.com/mailru/go-clickhouse/issues/151

英文:

Since version 2 of mailru/go-clickhouse it is possible to use both of them, authors have changed driver name to chhttp: https://github.com/mailru/go-clickhouse/issues/151

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

发表评论

匿名网友

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

确定