DynamoDB UpdateItem in Go (AWS SDK v2)
UpdateItem changes specific attributes of one item (and creates the item if it doesn't exist), leaving everything else untouched. In AWS SDK for Go v2 you call client.UpdateItem with an UpdateExpression on a dynamodb.UpdateItemInput.
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.UpdateItem(ctx, &dynamodb.UpdateItemInput{
TableName: aws.String("Music"),
Key: map[string]types.AttributeValue{
"Artist": &types.AttributeValueMemberS{Value: "Arturo Sandoval"},
"SongTitle": &types.AttributeValueMemberS{Value: "Cubano Chant"},
},
UpdateExpression: aws.String("SET #upd0 = :updValue0, #upd1 = :updValue1 ADD #upd2 :updValue2"),
ExpressionAttributeNames: map[string]string{
"#upd0": "Genre",
"#upd1": "Year",
"#upd2": "Awards",
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":updValue0": &types.AttributeValueMemberS{Value: "Latin Jazz"},
":updValue1": &types.AttributeValueMemberN{Value: "1994"},
":updValue2": &types.AttributeValueMemberN{Value: "1"},
},
ReturnValues: types.ReturnValueAllNew,
})
if err != nil {
log.Fatalf("update item: %v", err)
}
fmt.Println(out.Attributes) // the item after the update
}Explanation
Key— the full primary key of the item to update.UpdateExpression— one or more clauses:SETassigns attributes (#upd0 = :updValue0setsGenre;#upd1 = :updValue1setsYear). Every attribute is aliased viaExpressionAttributeNames, so reserved words likeYearare always safe.ADDon a number does an atomic increment (ADD #upd2 :updValue2adds 1 toAwards) — safe under concurrency, no read-modify-write race.REMOVEdeletes an attribute;DELETEremoves elements from a set.
ExpressionAttributeValues— the:placeholder→ typed-value map (AttributeValueMemberS/...MemberN).ReturnValues: types.ReturnValueAllNew— returns the full item after the update inout.Attributes(alsoReturnValueUpdatedNew,ReturnValueAllOld,ReturnValueUpdatedOld, orReturnValueNone).- Upsert semantics —
UpdateItemcreates the item if the key doesn't exist. Add aConditionExpression(e.g.attribute_exists(Artist)) to update-only. - Prefer building expressions in code? The
expressionpackage (github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression) composesSET/ADDclauses and generates the name/value maps for you.
Do it visually
The DynamoDB Expression Builder builds SET / ADD / REMOVE clauses with the name/value maps and copies runnable code.
To edit items in a GUI — change a field, review the generated UpdateExpression, copy the code — download DynoTable.
Related examples
- DynamoDB UpdateItem in Java — the same update with AWS SDK for Java 2.x.
- DynamoDB PutItem in Go — replace the whole item instead.
- DynamoDB update expressions —
SET,ADD,REMOVE,DELETE, and idioms. - Understanding ReturnValues — what each
ReturnValuesoption gives you. - "Attribute name is a reserved keyword" — why the alias map here isn't optional.
- "Invalid UpdateExpression" syntax errors — the common SET/ADD syntax mistakes, decoded.
References
- UpdateItem — Amazon DynamoDB API Reference
- Use UpdateItem 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)
- expression package — AWS SDK for Go v2 (pkg.go.dev)
- Update expressions — Amazon DynamoDB Developer Guide
Last verified 2026-07-13 against the official AWS documentation linked above.