Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Last active August 10, 2026 08:14
Show Gist options
  • Select an option

  • Save kavirajk/717c5898dbde1230ecb973972418da98 to your computer and use it in GitHub Desktop.

Select an option

Save kavirajk/717c5898dbde1230ecb973972418da98 to your computer and use it in GitHub Desktop.
query_param_clickhouse.go
package main
import (
"context"
"fmt"
"github.com/ClickHouse/ch-go"
"github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2"
)
func main() {
fmt.Println("clickhouse-go")
clickhouse_go()
fmt.Println("========")
fmt.Println("ch-go")
chgo()
}
func chgo() {
ctx := context.Background()
c, err := ch.Dial(ctx, ch.Options{Address: "localhost:9000"})
if err != nil {
panic(err)
}
var (
data proto.ColStr
)
params := ch.Parameters(map[string]any{
"str": "hello\\\\nworld",
})
if err := c.Do(ctx, ch.Query{
// Body: "SELECT number FROM system.numbers LIMIT 500000000",
Body: "SELECT {str: String} AS str",
Result: proto.Results{
{Name: "str", Data: &data},
},
Parameters: params,
}); err != nil {
panic(err)
}
fmt.Println("data", string(data.Buf))
}
func clickhouse_go() {
ctx := context.Background()
db, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{"127.0.0.1:9000"},
})
if err != nil {
panic(err)
}
// note the backquotes (`). not single quotes.
chCtx := clickhouse.Context(ctx, clickhouse.WithParameters(clickhouse.Parameters{
"str": `hello\nworld`,
}))
// you can also use double escaping
// chCtx := clickhouse.Context(ctx, clickhouse.WithParameters(clickhouse.Parameters{
// "str": "hello\\nworld",
// }))
row := db.QueryRow(chCtx, "SELECT {str: String}")
if err := row.Err(); err != nil {
panic(err)
}
var result string
if err := row.Scan(&result); err != nil {
panic(err)
}
fmt.Println("result: ", result)
}
@kavirajk

kavirajk commented Aug 10, 2026

Copy link
Copy Markdown
Author
$ go run string_query_param_chgo.go
clickhouse-go
result:  hello
world
========
ch-go
data hello
world

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment