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 withaws.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.GetItemwill not accept a partial key.ProjectionExpression— optional; limits which attributes come back (saves bandwidth, not read cost). Every projected attribute is aliased (#proj0…#proj3) viaExpressionAttributeNames, which keeps reserved words likeYearsafe.out.Item—nilwhen 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.
Related examples
- DynamoDB GetItem in Java — the same read with AWS SDK for Java 2.x.
- DynamoDB Query in Go — read a whole partition instead of one item.
- How DynamoDB partition keys work — why
GetItemneeds the full key. - DynamoDB ResourceNotFoundException — the usual first error here: wrong table name or region.
- "The provided key element does not match the schema" — the key you pass doesn't match the table's key schema.
References
- GetItem — Amazon DynamoDB API Reference
- Use GetItem with an AWS SDK or CLI — Amazon DynamoDB Developer Guide
- dynamodb package — AWS SDK for Go v2 (pkg.go.dev)
- dynamodb/types package — AWS SDK for Go v2 (pkg.go.dev)
- attributevalue package — AWS SDK for Go v2 (pkg.go.dev)
- Read consistency — Amazon DynamoDB Developer Guide
Last verified 2026-07-13 against the official AWS documentation linked above.