Go SDK

更新时间: 2026-06-10 11:03:44

Install and configure Tablestore SDK for Go to work with WideColumn, TimeSeries, and Timeline models.

Quick start

Install the SDK and initialize a Tablestore client.

Prerequisites

  • Go 1.4 or later installed. Download from the official Go download page.

  • A Go IDE such as GoLand or Visual Studio Code with the Go extension.

Install the SDK

Run go mod init <DIRNAME> in your project directory to generate a go.mod file, then install the SDK:

Note

<DIRNAME> is the path of your project directory.

go get github.com/aliyun/aliyun-tablestore-go-sdk/tablestore

Sample programs for each model are in the sample folder of the Tablestore SDK for Go repository.

Configure access credentials

Create an AccessKey for your Alibaba Cloud account or a RAM user. Then, configure the AccessKey in the environment variables as shown below. Configuring the AccessKey in environment variables improves security because it prevents you from hard-coding sensitive information in your code.

After the configuration is complete, you must restart or refresh your development environment. This includes your IDE, command-line interface, other desktop applications, and background services. This ensures that the latest system environment variables are loaded. For more information about other types of access credentials, see Configure access credentials.

Linux

  1. Run the following commands to append the environment variable settings to the ~/.bashrc file.

    echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc
    echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc
  2. Run the following command to apply the changes.

    source ~/.bashrc
  3. Run the following commands to verify that the environment variables are effective.

    echo $TABLESTORE_ACCESS_KEY_ID
    echo $TABLESTORE_ACCESS_KEY_SECRET

macOS

  1. Run the following command in the terminal to view your default shell type.

    echo $SHELL
  2. Perform the following operations based on your default shell type.

    Zsh
    1. Run the following commands to append the environment variable settings to the ~/.zshrc file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc
    2. Run the following command to apply the changes.

      source ~/.zshrc
    3. Run the following commands to verify that the environment variables are effective.

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET
    Bash
    1. Run the following commands to append the environment variable settings to the ~/.bash_profile file.

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile
    2. Run the following command to apply the changes.

      source ~/.bash_profile
    3. Run the following commands to verify that the environment variables are effective.

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET

Windows

CMD
  1. Run the following commands in Command Prompt (CMD) to set the environment variables.

    setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID"
    setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"
  2. Restart CMD and run the following commands to verify that the environment variables are effective.

    echo %TABLESTORE_ACCESS_KEY_ID%
    echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
  1. Run the following commands in PowerShell.

    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
  2. Run the following commands to verify that the environment variables are effective.

    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)

Initialize a client

After initializing a client, list the tables in your instance to verify the connection.

Important

Public network access is disabled by default for new instances. To access instance resources over the public network, enable public network access in Network Management of your instance.

WideColumn

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)

func main() {
	// yourInstanceName: enter your instance name
	instanceName := "yourInstanceName"
	// yourEndpoint: enter the endpoint of your instance
	endpoint := "yourEndpoint"
	// Obtain the AccessKey ID and AccessKey Secret from environment variables
	accessKeyId := os.Getenv("TABLESTORE_ACCESS_KEY_ID")
	accessKeySecret := os.Getenv("TABLESTORE_ACCESS_KEY_SECRET")

	// Initialize the Tablestore client
	client := tablestore.NewClient(endpoint, instanceName, accessKeyId, accessKeySecret)

	// List data tables in the instance and print to console
	tables, err := client.ListTable()
	if err != nil {
		fmt.Println("Failed to list table")
	} else {
		for _, table := range tables.TableNames {
			fmt.Println(table)
		}
	}
}

TimeSeries

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)

func main() {
	// yourInstanceName: enter your instance name
	instanceName := "yourInstanceName"
	// yourEndpoint: enter the endpoint of your instance
	endpoint := "yourEndpoint"
	// Obtain the AccessKey ID and AccessKey Secret from environment variables
	accessKeyId := os.Getenv("TABLESTORE_ACCESS_KEY_ID")
	accessKeySecret := os.Getenv("TABLESTORE_ACCESS_KEY_SECRET")

	// Initialize the Tablestore client
	client := tablestore.NewTimeseriesClient(endpoint, instanceName, accessKeyId, accessKeySecret)

	// List time series tables in the instance and print to console
	timeseriesTables, err := client.ListTimeseriesTable()
	if err != nil {
		fmt.Println("Failed to list table")
	} else {
		for _, timeseriesTablesMeta := range timeseriesTables.GetTimeseriesTableMeta() {
			fmt.Println(timeseriesTablesMeta.GetTimeseriesTableName())
		}
	}
}

Version compatibility

The latest version is 1.17.x. All versions are backward compatible.

For the SDK changelog, see Version history of Tablestore SDK for Go.

FAQ

References

For error handling in Tablestore, see Error handling.

上一篇: Error handling 下一篇: Operations on data tables
阿里云首页 表格存储 相关技术圈