How to Connect to DynamoDB from Go

Connect to DynamoDB from Go by initializing the AWS SDK v2 client and calling GetItem with your table name and primary key.

Use the AWS SDK for Go v2 to create a DynamoDB client with your region and credentials, then call GetItem to retrieve data.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

func main() {
	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		log.Fatalf("unable to load SDK config: %v", err)
	}

	client := dynamodb.NewFromConfig(cfg)

	input := &dynamodb.GetItemInput{
		TableName: aws.String("MyTable"),
		Key: map[string]types.AttributeValue{
			"pk": &types.StringValue{Value: "user123"},
		},
	}

	result, err := client.GetItem(context.TODO(), input)
	if err != nil {
		log.Fatalf("unable to get item: %v", err)
	}

	fmt.Printf("Item: %+v\n", result.Item)
}

Note: Add "github.com/aws/aws-sdk-go-v2/aws" to imports for aws.String.