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