# Go Client

MyScale is compatible with ClickHouse, so you can use the official Clickhouse Go client (opens new window) to access MyScale from your Go applications.

# Install

go get -u github.com/ClickHouse/clickhouse-go/v2

# Examples

# Connecting to Database

To learn how to establish a connection to the cluster, please refer to the Connection Details section.

// Replace "MYSCALE_CLUSTER_URL" with the endpoint hostname of your cluster,
// which can be found in the MyScale web console.
// An example of the hostname is "msc-ad2378cf.us-east-1.aws.myscale.com"
// Here "default" is your default database, and you can replace "default"
// with the name of your target database.
conn:= clickhouse.OpenDB(&clickhouse.Options{
    Addr: []string{"MYSCALE_CLUSTER_URL"},
    Auth: clickhouse.Auth{
        Database: "default",
        Username: "YOUR_USERNAME",
        Password: "YOUR_CLUSTER_PASSWORD",
    },
    Protocol:  clickhouse.HTTP,
    TLS: &tls.Config{
        InsecureSkipVerify: false,
    },
})
_, err := conn.Query("SELECT 1")
if err != nil {
    fmt.Printf("err %s", err)
    return
}

# Importing Data

Create a table with vectors:

_, err = conn.Exec("CREATE TABLE default.myscale_categorical_search" +
    "(" +
    "    id    UInt32," +
    "    data  Array(Float32)," +
    "    CONSTRAINT check_length CHECK length(data) = 128," +
    "    date  Date," +
    "    label Enum8('person' = 1, 'building' = 2, 'animal' = 3)" +
    ")" +
    "ORDER BY id")
if err != nil {
    fmt.Printf("err %s", err)
    return
}

Assuming we have a set of data with a defined data structure:

type DataItem struct {
    ID    int
    Data  []float32
    Date  string
    Label string
}
data := []DataItem{
    {
        ID:    0,
        Data:  []float32{0, 0, 0, 0.01, 0.08, 0.07, 0.03, 0.02, 0.05, 0, 0, 0.03, 0.05, 0.07, 0.11, 0.31, 0.13, 0, 0, 0, 0, 0.29, 1.06, 1.07, 0.13, 0, 0, 0, 0.01, 0.61, 0.7, 0.42, 0, 0, 0, 0, 0.01, 0.23, 0.28, 0.16, 0.63, 0.04, 0, 0, 0, 0.06, 0.83, 0.81, 1.17, 0.86, 0.25, 0.15, 0.17, 0.5, 0.84, 1.17, 0.31, 0.23, 0.18, 0.35, 0.97, 1.17, 0.49, 0.24, 0.68, 0.27, 0, 0, 0, 0.04, 0.29, 0.71, 0.81, 0.47, 0.13, 0.1, 0.32, 0.87, 1.17, 1.17, 0.45, 0.76, 0.4, 0.22, 0.6, 0.7, 0.41, 0.09, 0.07, 0.21, 0.29, 0.39, 0.53, 0.21, 0.04, 0.01, 0.55, 0.72, 0.03, 0, 0, 0, 0, 0.09, 0.65, 1.17, 0.73, 0.37, 0.28, 0.23, 0.17, 0.34, 0.11, 0.11, 0.27, 0.61, 0.64, 0.25, 0.04, 0, 0.42, 0.13, 0.01, 0.01, 0.01, 0.14, 0.1, 0.06},
        Date:  "2030-09-26",
        Label: "person",
    },
    {
        ID:    1,
        Data:  []float32{0.65, 0.35, 0.08, 0, 0, 0, 0.01, 0.63, 0.48, 0.27, 0.31, 0.19, 0.16, 0.34, 0.96, 1.14, 0.03, 0.01, 0.08, 0.21, 0.27, 0.43, 0.57, 0.21, 0.11, 0.08, 0.37, 0.08, 0, 0, 0.01, 0.23, 1.01, 1.04, 0.11, 0, 0, 0, 0, 0.29, 0.83, 1.14, 1.14, 0.77, 0.23, 0.14, 0.18, 0.52, 0.28, 0.08, 0.46, 0.75, 0.39, 0.24, 0.59, 0.6, 0.02, 0, 0.18, 0.1, 0.2, 0.52, 0.52, 0.16, 0.12, 0.28, 0.04, 0, 0, 0.03, 0.05, 0.08, 1.02, 0.79, 0.58, 0.03, 0, 0, 0, 0.11, 1.14, 1.12, 0.78, 0.5, 0.17, 0.14, 0.45, 1.04, 0.19, 0.31, 0.53, 1.14, 0.73, 0.44, 0.34, 0.26, 0.03, 0.02, 0, 0, 0, 0.01, 0.08, 0.09, 0.34, 0.2, 0, 0, 0, 0, 0.01, 0.23, 0.3, 0.75, 0.87, 0.36, 0, 0, 0, 0.02, 0, 0.17, 0.66, 0.73, 0.03, 0, 0, 0},
        Date:  "1996-06-22",
        Label: "building",
    },
    {
        ID:    2,
        Data:  []float32{0, 0, 0, 0, 0, 0, 0.04, 0.01, 0.15, 0, 0, 0, 0, 0, 0.1, 0.49, 0.27, 0, 0, 0, 0, 0.29, 1.13, 1.14, 0.09, 0, 0, 0, 0.03, 0.69, 0.71, 0.42, 0.14, 0, 0, 0, 0, 0.01, 0.56, 0.79, 0.63, 0.02, 0, 0, 0, 0.38, 1.18, 0.77, 1.18, 0.6, 0.08, 0.08, 0.18, 0.48, 0.59, 1.04, 0.27, 0.16, 0.07, 0.13, 0.8, 1.18, 0.34, 0.21, 1.18, 0.47, 0.04, 0, 0, 0.01, 0.32, 0.99, 0.61, 0.4, 0.31, 0.57, 0.46, 1.18, 1.18, 0.61, 0.8, 0.64, 0.16, 0.21, 0.2, 0.33, 0.23, 0.27, 0.06, 0.22, 0.16, 0.14, 0.51, 0.33, 0, 0, 0.76, 0.4, 0.08, 0, 0.02, 0.14, 0.42, 0.94, 0.19, 0.42, 0.57, 0.67, 0.23, 0.34, 0.22, 0.1, 0.09, 0.52, 0.15, 0.21, 0.05, 0.01, 0.03, 0.03, 0.01, 0.38, 0.12, 0.05, 0.18, 0.01, 0, 0},
        Date:  "2015-12-17",
        Label: "person",
    },
    {
        ID:    3,
        Data:  []float32{0.03, 0.09, 0.45, 0.22, 0.28, 0.11, 0.04, 0.03, 0.77, 0.1, 0.04, 0.01, 0.01, 0.04, 0.03, 0.11, 0.23, 0, 0, 0, 0.26, 0.49, 0.06, 0.07, 0.05, 0.03, 0.03, 0.01, 0.11, 0.5, 0.08, 0.09, 0.11, 0.07, 0.15, 0.21, 0.12, 0.17, 0.21, 0.25, 1.21, 0.12, 0.04, 0.07, 0.04, 0.07, 0.04, 0.41, 0.28, 0.02, 0, 0.01, 0.1, 0.42, 0.22, 0.2, 0.01, 0.01, 0.04, 0.09, 0.31, 0.79, 0.16, 0.03, 0.23, 0.04, 0.06, 0.26, 0.31, 1.21, 0.87, 0.4, 1.21, 0.82, 0.16, 0.12, 0.15, 0.41, 0.06, 0.1, 0.76, 0.48, 0.05, 0.03, 0.21, 0.42, 0.41, 0.5, 0.05, 0.17, 0.18, 0.64, 0.86, 0.54, 0.17, 0.06, 0.43, 0.62, 0.56, 0.84, 1.16, 1.08, 0.38, 0.26, 0.58, 0.63, 0.2, 0.87, 1.05, 0.37, 0.02, 0.02, 1.21, 1.21, 0.38, 0.25, 0.44, 0.33, 0.24, 0.46, 0.03, 0.16, 0.27, 0.74, 1.21, 0.55, 0.09, 0.04},
        Date:  "2024-08-11",
        Label: "animal",
    },
    {
        ID:    4,
        Data:  []float32{0.06, 0.04, 0.03, 0.07, 0.8, 1.22, 0.62, 0.19, 0.02, 0, 0, 0, 0.32, 0.6, 0.1, 0.19, 0.04, 0, 0, 0, 0, 0.1, 0.69, 0.66, 0, 0, 0, 0, 0.08, 0.58, 0.49, 0.05, 0.05, 0.31, 0.59, 0.67, 1.22, 0.37, 0.01, 0.02, 0.5, 0.01, 0, 0.16, 0.99, 0.48, 0.03, 0.27, 1.22, 0.38, 0.06, 0.07, 0.11, 0.31, 0.87, 1.22, 0.09, 0.08, 0.06, 0.23, 1.22, 1.22, 0.69, 0.21, 0, 0.11, 0.31, 0.55, 0.28, 0, 0, 0, 0.61, 0.04, 0, 0.37, 0.43, 0.02, 0, 0.15, 1.22, 1.22, 0.55, 0.32, 0.06, 0.01, 0, 0.12, 0.05, 0.22, 0.52, 1.22, 1.22, 0.09, 0.02, 0, 0.02, 0, 0, 0.05, 0.28, 0.2, 0.02, 0.02, 0.19, 0.03, 0, 0.02, 0.12, 0.12, 0.03, 0.16, 0.25, 0.18, 0.34, 0.35, 0.05, 0.04, 0.01, 0.13, 0.21, 0.02, 0.22, 0.51, 0.09, 0.2, 0.57, 0.59},
        Date:  "1970-01-31",
        Label: "animal",
    },
    {
        ID:    5,
        Data:  []float32{0.06, 0.02, 0.19, 0.22, 0.22, 0.81, 0.31, 0.12, 0.72, 0.15, 0.12, 0.1, 0.03, 0.06, 0.01, 0.37, 0.3, 0.17, 0.04, 0.02, 0.09, 0.04, 0.02, 0.21, 0.01, 0, 0.01, 0.03, 0.11, 0.09, 0.05, 0.02, 0.07, 0.11, 0.17, 0.61, 1.27, 1.27, 0.28, 0.13, 0.49, 0.36, 0.26, 0.45, 0.28, 0.17, 0.04, 0.16, 1.11, 0.46, 0.11, 0.02, 0.07, 0.25, 0.4, 0.89, 0.02, 0, 0.08, 0.31, 0.63, 0.6, 0.28, 0.12, 0, 0.18, 0.82, 1.27, 0.5, 0.01, 0, 0, 0.94, 0.28, 0.11, 0.88, 0.15, 0, 0, 0.04, 1.27, 1.27, 0.34, 0.23, 0.25, 0.18, 0.18, 0.69, 0.06, 0.16, 0.26, 0.9, 1.27, 0.42, 0.12, 0.08, 0, 0.03, 0.46, 0.29, 0, 0, 0, 0, 0.22, 0.35, 0.15, 0.12, 0, 0, 0, 0, 0.46, 1.27, 0.83, 0.17, 0.01, 0, 0, 0, 0, 0.14, 0.67, 1.15, 0.45, 0, 0, 0},
        Date:  "2025-04-02",
        Label: "building",
    },
    {
        ID:    6,
        Data:  []float32{0.19, 0.35, 0.05, 0.06, 0.4, 0.23, 0.18, 0.04, 0.21, 1.09, 1.2, 0.23, 0.05, 0.12, 0.24, 0.05, 0, 0.05, 0.87, 1.08, 0.47, 0.14, 0.32, 0.08, 0, 0, 0, 0.27, 0.36, 0.3, 0.43, 0, 0.29, 0.12, 0.1, 0.15, 0.06, 0.07, 0.17, 0.12, 0.34, 0.09, 0.14, 0.65, 0.2, 0.23, 0.28, 0.14, 1.2, 0.34, 0.14, 0.14, 0.09, 0.34, 1.2, 1.2, 0.07, 0.06, 0.07, 0.27, 0.56, 1.2, 1.2, 0.23, 0.09, 0.05, 0.04, 0.07, 0.02, 0.06, 0.46, 0.13, 0.29, 0.05, 0.05, 0.32, 0.12, 0.2, 0.99, 0.19, 1.2, 1.2, 1.07, 0.38, 0.13, 0.07, 0.24, 0.36, 0.06, 0.24, 1.2, 1.2, 0.55, 0.26, 0.04, 0.03, 0.05, 0.01, 0, 0, 0.01, 0.05, 0.19, 0.18, 0.02, 0.02, 0, 0.01, 0.18, 0.12, 0.3, 0.07, 0, 0.05, 0.33, 0.29, 0.66, 0.5, 0.26, 0.02, 0, 0, 0.49, 0.45, 0.12, 0.28, 0.1, 0},
        Date:  "2007-06-29",
        Label: "animal",
    },
    {
        ID:    7,
        Data:  []float32{0.28, 0.28, 0.28, 0.27, 0.13, 0.05, 0.04, 0.12, 0.04, 0.08, 0.29, 1.18, 0.69, 0.19, 0.21, 0.07, 0.03, 0, 0, 0.14, 0.14, 0.1, 1.05, 0.6, 0, 0, 0, 0, 0.11, 0.69, 0.76, 0.09, 0.05, 0.02, 0.18, 0.59, 0.17, 0.06, 0.01, 0.05, 0.42, 0.09, 0.16, 0.75, 0.31, 0.21, 0.17, 0.13, 1.18, 0.44, 0.18, 0.16, 0.17, 0.3, 0.78, 1.18, 0.04, 0.04, 0.08, 0.61, 1.18, 1.1, 0.54, 0.25, 0.1, 0.06, 0.21, 0.54, 0.05, 0.05, 0.06, 0.05, 0.38, 0.17, 0.11, 0.31, 0.06, 0.24, 0.64, 0.15, 1.15, 1.18, 1.17, 0.61, 0.13, 0.13, 0.22, 0.25, 0.02, 0.11, 0.66, 1.18, 0.87, 0.25, 0.1, 0.02, 0.1, 0.11, 0.03, 0.02, 0.09, 0.28, 0.04, 0.05, 0.21, 0.18, 0.35, 0.17, 0.06, 0.1, 0.04, 0.3, 0.2, 0.02, 0.13, 0.13, 0.07, 0.3, 0.71, 1.18, 0, 0, 0.03, 0.12, 0.5, 1.03, 0.44, 0.05},
        Date:  "1970-09-10",
        Label: "building",
    },
    {
        ID:    8,
        Data:  []float32{0.41, 0.38, 0.21, 0.17, 0.42, 0.71, 0.6, 0.5, 0.11, 0.01, 0.02, 0.11, 1.09, 1.15, 0.08, 0.04, 0.27, 0.08, 0.05, 0.22, 0.11, 0.09, 0.08, 0.14, 0.2, 0.1, 0.04, 0.33, 0.12, 0.07, 0.04, 0.01, 0.18, 1.15, 0.95, 0.42, 0.17, 0.01, 0, 0, 0.19, 0.06, 0.46, 1.15, 0.91, 0.16, 0, 0.07, 0.66, 0.07, 0.04, 0.15, 0.12, 0.32, 0.91, 1.09, 0.12, 0.03, 0.01, 0.08, 0.21, 1.15, 0.96, 0.17, 0.01, 0.51, 0.78, 0.14, 0, 0, 0, 0, 0.5, 0.4, 0.62, 0.53, 0, 0, 0, 0.03, 1.15, 1.15, 0.4, 0.12, 0.06, 0.13, 0.25, 0.65, 0.07, 0.3, 0.51, 0.65, 1.1, 0.92, 0.25, 0.09, 0, 0.01, 0.13, 0, 0, 0, 0, 0, 0.04, 0.22, 0.11, 0.01, 0, 0, 0, 0, 0.13, 1.15, 0.48, 0.01, 0, 0, 0, 0, 0, 0.36, 1.02, 0.63, 0.11, 0, 0, 0},
        Date:  "2007-10-26",
        Label: "person",
    },
    {
        ID:    9,
        Data:  []float32{0, 0, 0, 0, 0, 0.02, 0.06, 0.04, 0, 0, 0, 0, 0, 0.01, 0.44, 0.57, 0, 0, 0, 0, 0, 0.15, 1.25, 0.52, 0, 0, 0, 0, 0.06, 0.57, 0.44, 0.02, 0.23, 0.01, 0, 0, 0, 0.06, 0.2, 0.23, 1.25, 0.3, 0.05, 0.02, 0.01, 0.03, 0.73, 1.25, 0.16, 0.1, 0.11, 0.46, 0.61, 0.97, 1.25, 0.93, 0, 0, 0, 0.31, 1.11, 0.96, 0.21, 0, 0.2, 0.06, 0, 0, 0.09, 1.14, 0.63, 0.05, 1.25, 1.25, 0.83, 0.08, 0.02, 0.26, 0.05, 0.23, 0.14, 0.56, 1.25, 1.25, 0.37, 0.1, 0.07, 0.1, 0.11, 0.02, 0.17, 0.87, 0.42, 0.05, 0.08, 0.19, 0, 0, 0.07, 0.32, 0.56, 0.91, 0.08, 0, 0.01, 0.17, 0.17, 0.03, 0.14, 0.71, 0.15, 0.05, 0.07, 0.09, 0.35, 0.1, 0.02, 0.05, 0.24, 0.39, 0.14, 0.16, 0.04, 0.09, 0.22, 0.06, 0.13, 0.11},
        Date:  "1971-02-02",
        Label: "building",
    },
}

Use Exec function to batch write data:

scope, err := conn.Begin()
if err != nil {
    fmt.Printf("err %s", err)
    return
}
batch, err := scope.Prepare("INSERT INTO default.myscale_categorical_search")
if err != nil {
    fmt.Printf("err %s", err)
    return
}
for i := 0; i < len(data); i++ {
    _, err := batch.Exec(
      uint32(data[i].ID),
      data[i].Data,
      data[i].Date,
      data[i].Label,
    )
    if err != nil {
        fmt.Printf("err %s", err)
        return
    }
}
err = scope.Commit()
if err != nil {
    fmt.Printf("err %s", err)
    return
}

Create a vector search index:

_, err = conn.Exec("ALTER TABLE default.myscale_categorical_search ADD VECTOR INDEX categorical_vector_idx data TYPE MSTG")
if err != nil {
    fmt.Printf("err %s", err)
    return
}

Search for vectors and release the result set:

rows, err := conn.Query("SELECT id, date, label, data," + "distance(data, [3.0,9,45,22,28,11,4,3,77,10,4,1,1,4,3,11,23,0," +
    "0,0,26,49,6,7,5,3,3,1,11,50,8,9,11,7,15,21,12,17,21,25,121,12,4,7,4,7,4," +
    "41,28,2,0,1,10,42,22,20,1,1,4,9,31,79,16,3,23,4,6,26,31,121,87,40,121,82," +
    "16,12,15,41,6,10,76,48,5,3,21,42,41,50,5,17,18,64,86,54,17,6,43,62,56,84," +
    "116,108,38,26,58,63,20,87,105,37,2,2,121,121,38,25,44,33,24,46,3,16,27,74," +
    "121,55,9,4]) AS dist " +
    "FROM default.myscale_categorical_search ORDER BY dist LIMIT 10")
if err != nil {
    fmt.Printf("err %s", err)
    return
}
for rows.Next() {
    var (
        id    int
        date  string
        label string
        data  []float32
        dist  float32
    )
    if err := rows.Scan(&id, &date, &label, &data, &dist); err != nil {
        fmt.Printf("err %s", err)
        return
    }
    fmt.Printf("row: id=%d, date=%s, label=%s, dist=%f\n", id, date, label, dist)
}

Output:

row: id=3, date=2024-08-11T00:00:00+08:00, label=animal, dist=252972.640625
row: id=5, date=2025-04-02T00:00:00+08:00, label=building, dist=255089.046875
row: id=9, date=2024-08-11T00:00:00+08:00, label=person, dist=255117.093750
row: id=9, date=1971-02-02T00:00:00+08:00, label=building, dist=255117.093750
row: id=2, date=2015-12-17T00:00:00+08:00, label=person, dist=255440.062500
row: id=0, date=2030-09-26T00:00:00+08:00, label=person, dist=255492.390625
row: id=1, date=1996-06-22T00:00:00+08:00, label=building, dist=255535.312500
row: id=7, date=1970-09-10T00:00:00+08:00, label=building, dist=255631.140625
row: id=4, date=1970-01-31T00:00:00+08:00, label=animal, dist=255742.671875
row: id=8, date=2007-10-26T00:00:00+08:00, label=person, dist=255818.390625

At last, close the connection:

conn.Close()