Skip to content

Instantly share code, notes, and snippets.

@dzzzchhh
Created January 31, 2022 16:47
Show Gist options
  • Save dzzzchhh/99322bdfaf8e8a2b83f7167bedcfd7a9 to your computer and use it in GitHub Desktop.
Save dzzzchhh/99322bdfaf8e8a2b83f7167bedcfd7a9 to your computer and use it in GitHub Desktop.
AWS DynamoDB in go (example)
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
func listTables(svc *dynamodb.Client) {
resp, err := svc.ListTables(context.TODO(), &dynamodb.ListTablesInput{
Limit: aws.Int32(5),
})
if err != nil {
log.Fatalf("failed to list tables, %v", err)
}
fmt.Println("Tables:")
for _, tableName := range resp.TableNames {
fmt.Println(tableName)
}
}
func createTable(svc *dynamodb.Client) {
resp, err := svc.CreateTable(context.TODO(), &dynamodb.CreateTableInput{
TableName: aws.String("table name"),
BillingMode: types.BillingModeProvisioned,
AttributeDefinitions: []types.AttributeDefinition{
{
AttributeName: aws.String("Title"),
AttributeType: types.ScalarAttributeTypeS,
},
{
AttributeName: aws.String("Description"),
AttributeType: types.ScalarAttributeTypeS,
},
},
KeySchema: []types.KeySchemaElement{
{
// TODO: read more from here
AttributeName: aws.String("Title"),
KeyType: types.KeyTypeHash,
},
{
AttributeName: aws.String("Description"),
KeyType: types.KeyTypeHash,
},
},
ProvisionedThroughput: &types.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(1),
WriteCapacityUnits: aws.Int64(1),
},
})
if err != nil {
log.Fatalf("failed to create table %v", err)
}
fmt.Println(resp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment