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:
<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 RAM user, then configure it as an environment variable to avoid hard-coding credentials.
Restart your IDE, terminal, other desktop applications, and background services after configuration to load the updated environment variables. For more information about other types of access credentials, see Configure access credentials.
Linux
-
Append the environment variables to
~/.bashrc:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc -
Apply the changes:
source ~/.bashrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
macOS
-
Check your default shell:
echo $SHELL -
Configure based on your shell type:
Zsh
-
Append the environment variables to
~/.zshrc:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc -
Apply the changes:
source ~/.zshrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Bash
-
Append the environment variables to
~/.bash_profile:echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile -
Apply the changes:
source ~/.bash_profile -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
-
Windows
CMD
-
Set the environment variables in CMD:
setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET" -
Restart CMD and verify:
echo %TABLESTORE_ACCESS_KEY_ID% echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
-
Run 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) -
Verify the environment variables:
[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.
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 Go SDK error handling.