All Products
Search
Document Center

Tablestore:Query data with the Go driver

Last Updated:Jun 05, 2026

The Go driver (aliyun-tablestore-go-sql-driver) implements the database/sql/driver interface, enabling you to access Tablestore data through the standard database/sql package.

Prerequisites

  • An AccessKey pair with the ots:SQL* permission (required for RAM users).

  • A data table and a mapping table. For more information, see DDL operations.

Procedure

Step 1: Install the Go driver

Run the following command to install the Go driver:

go get github.com/aliyun/aliyun-tablestore-go-sql-driver

Step 2: Connect with the Go driver

Parameters

To access Tablestore with the Go driver, specify the driver name and a Data Source Name (DSN).

Parameter

Description

driverName

The Tablestore Go driver name. Set this to ots.

dataSourceName

The DSN, in the format schema://accessKeyId:accessKeySecret@endpoint/instanceName[?param1=value1&...&paramN=valueN]. Key fields:

  • schema (required): The protocol. Usually https.

  • accessKeyId:accessKeySecret (required): The AccessKey ID and AccessKey Secret of your Alibaba Cloud account or RAM user.

  • endpoint (required): The endpoint of your Tablestore instance.

  • instanceName (required): The name of your Tablestore instance.

For other configuration options, see Configuration options.

Example

import (
    "database/sql"
    _ "github.com/aliyun/aliyun-tablestore-go-sql-driver"
)

// Set the Go driver name and the Tablestore data source name.
db, err := sql.Open("ots", "https://access_key_id:access_key_secret@endpoint/instance_name")
if err != nil {
    panic(err)    // Handle the error.
}

Step 3: Query data

Run queries with the Query method or use prepared statements with the Prepare method.

Note

Variable types must match Tablestore data types. For the type mapping, see Data type mapping.

Query

rows, err := db.Query("SELECT pk1, col1, col2 FROM test_table WHERE pk1 = ?", 3)
if err != nil {
    panic(err)
}
defer rows.Close()
for rows.Next() {
    var pk1 int64
    var col1 float64
    var col2 string
    if err := rows.Scan(&pk1, &col1, &col2); err != nil {
        panic(err)
    }
    fmt.Println(pk1, col1, col2)
}

Prepare

stmt, err := db.Prepare("SELECT pk1, col1, col2 FROM test_table WHERE pk1 = ?")
if err != nil {
    panic(err)
}
defer stmt.Close()
rows, err := stmt.Query(3)
if err != nil {
    panic(err)
}
defer rows.Close()
for rows.Next() {
    var pk1 int64
    var col1 float64
    var col2 string
    if err := rows.Scan(&pk1, &col1, &col2); err != nil {
        panic(err)
    }
    fmt.Println(pk1, col1, col2)
}

Complete example

The following example connects to a Tablestore instance and queries all data from test_table:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/aliyun/aliyun-tablestore-go-sql-driver"
)

func main() {
    db, err := sql.Open("ots", "https://************************:********************************@myinstance.cn-hangzhou.ots.aliyuncs.com/myinstance")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    rows, err := db.Query("SELECT * FROM test_table")
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    columns, _ := rows.Columns()
    for rows.Next() {
        values := make([]interface{}, len(columns))
        pointers := make([]interface{}, len(columns))
        for i := range values {
            pointers[i] = &values[i]
        }
        if err := rows.Scan(pointers...); err != nil {
            panic(err)
        }
        fmt.Println(values...)
    }
}

Configuration options

Configure the Go driver by adding query parameters to the DSN.

https://ak:sk@endpoint/instance?retryTimes=3&requestTimeout=10s

Time-based options use Go duration format, such as 15s and 500ms.

Option

Default

Description

retryTimes

10

Maximum number of retries. Integer.

connectionTimeout

15s

Connection timeout. 0 means no timeout.

requestTimeout

30s

Request timeout.

maxRetryTime

5s

Maximum retry interval.

maxIdleConnections

2000

Maximum number of idle connections. Integer.

Data type mapping

Tablestore data types map to Go types. A type mismatch causes an error.

Tablestore data type

Go data type

Integer

int64

Binary

[]byte

String

string

Double

float64

Boolean

bool