Go SDK
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 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
-
Run the following commands to append the environment variable settings to the
~/.bashrcfile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc -
Run the following command to apply the changes.
source ~/.bashrc -
Run the following commands to verify that the environment variables are effective.
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
macOS
-
Run the following command in the terminal to view your default shell type.
echo $SHELL -
Perform the following operations based on your default shell type.
Zsh
-
Run the following commands to append the environment variable settings to the
~/.zshrcfile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc -
Run the following command to apply the changes.
source ~/.zshrc -
Run the following commands to verify that the environment variables are effective.
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Bash
-
Run the following commands to append the environment variable settings to the
~/.bash_profilefile.echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile -
Run the following command to apply the changes.
source ~/.bash_profile -
Run the following commands to verify that the environment variables are effective.
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
-
Windows
CMD
-
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" -
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
-
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) -
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.
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.