当前位置: > > > > 当驱动程序具有相同名称时,在 Golang 中运行时选择 SQL 驱动程序
来源:stackoverflow
2024-04-21 23:06:24
0浏览
收藏
哈喽!今天心血来潮给大家带来了《当驱动程序具有相同名称时,在 Golang 中运行时选择 SQL 驱动程序》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!
问题内容
我想知道当这两个驱动程序具有相同名称时,是否有一种方法或投影模式能够在 golang 中运行时选择 sql 驱动程序。我想使用环境变量在 http clickhouse 驱动程序 (https://github.com/mailru/go-clickhouse) 和本机 tcp clickhouse 驱动程序 (https://github.com/clickhouse/clickhouse-go) 之间切换。 p>
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)
通常,它会导致“恐慌:sql:为驱动程序 clickhouse 调用两次注册”。有可能避免这种情况吗?
正确答案
从 mailru/go-clickhouse 版本 2 开始,可以同时使用它们,作者已将驱动程序名称更改为 chhttp
:
好了,本文到此结束,带大家了解了《当驱动程序具有相同名称时,在 Golang 中运行时选择 SQL 驱动程序》,希望本文对你有所帮助!关注公众号,给大家分享更多Golang知识!