DynamoDB GetItem in Go (AWS SDK v2)

GetItem reads a single item by its full primary key. In AWS SDK for Go v2 you call client.GetItem with a dynamodb.GetItemInput; key values are typed members from the types package (&types.AttributeValueMemberS{...} for strings, ...MemberN for numbers).

Code

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"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() {
	ctx := context.TODO()
	cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("us-east-1"))
	if err != nil {
		log.Fatalf("load config: %v", err)
	}
	client := dynamodb.NewFromConfig(cfg)

	out, err := client.GetItem(ctx, &dynamodb.GetItemInput{
		TableName: aws.String("Music"),
		Key: map[string]types.AttributeValue{
			"Artist":    &types.AttributeValueMemberS{Value: "Arturo Sandoval"},
			"SongTitle": &types.AttributeValueMemberS{Value: "Cubano Chant"},
		},
		ProjectionExpression: aws.String("#proj0, #proj1, #proj2, #proj3"),
		ExpressionAttributeNames: map[string]string{
			"#proj0": "Artist",
			"#proj1": "SongTitle",
			"#proj2": "AlbumTitle",
			"#proj3": "Year",
		},
	})
	if err != nil {
		log.Fatalf("get item: %v", err)
	}

	if out.Item == nil {
		fmt.Println("Item not found")
		return
	}
	fmt.Println(out.Item)
}

To unmarshal out.Item into a plain Go struct, use attributevalue.UnmarshalMap from github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue (with dynamodbav struct tags).

Explanation

  • TableName — the table to read from; scalar input fields are pointers, so wrap literals with aws.String(...).
  • Key — must include every primary-key attribute: the partition key alone for a simple key, or partition and sort key for a composite key. GetItem will not accept a partial key.
  • ProjectionExpression — optional; limits which attributes come back (saves bandwidth, not read cost). Every projected attribute is aliased (#proj0#proj3) via ExpressionAttributeNames, which keeps reserved words like Year safe.
  • out.Itemnil when no item matches; always check before using it.
  • By default reads are eventually consistent. Set ConsistentRead: aws.Bool(true) for a strongly-consistent read (costs 2× the RCU, base table only).

Do it visually

Building the projection or a conditional read by hand? Use the free DynamoDB Expression Builder to assemble the expression and copy the exact ProjectionExpression / ExpressionAttributeNames values.

To browse tables and run GetItem in a GUI — key form, results grid, one-click copy of the generated code — download DynoTable.

References

Last verified 2026-07-13 against the official AWS documentation linked above.

Work with DynamoDB without the Console

DynoTable is a fast desktop client for DynamoDB — browse tables, run SQL-style queries, and edit items locally.