Last active
August 10, 2026 08:14
-
-
Save kavirajk/717c5898dbde1230ecb973972418da98 to your computer and use it in GitHub Desktop.
query_param_clickhouse.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.