Listing all Amazon DynamoDB Tables Using the AWS SDK for Go
The following example uses the DynamoDBListTables operation
to list all tables for the us-west-2
region.
Create the file dynamodb_list_tables.go. Add the following statements to import the Go and AWS SDK for Go packages used in the example.
import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" )
Initialize the session that the SDK uses to load credentials from the shared credentials file ~/.aws/credentials, and create a new DynamoDB service client.
sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create DynamoDB client svc := dynamodb.New(sess)
Call ListTables. If an error occurs, print the error and exit. If no error occurs, loop through the tabless, printing the name of each table.
result, err := svc.ListTables(&dynamodb.ListTablesInput{}) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println("Tables:") fmt.Println("") for _, n := range result.TableNames { fmt.Println(*n) }
See the complete example on GitHub.