Search code examples
gomicroservicesclickhouseclickhouse-go

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


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?


Solution

  • 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