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:
    • SET assigns attributes (#upd0 = :updValue0 sets Genre; #upd1 = :updValue1 sets Year). Every attribute is aliased via ExpressionAttributeNames, so reserved words like Year are always safe.
    • ADD on a number does an atomic increment (ADD #upd2 :updValue2 adds 1 to Awards) — safe under concurrency, no read-modify-write race. REMOVE deletes an attribute; DELETE removes elements from a set.
  • ExpressionAttributeValues — the :placeholder → typed-value map (AttributeValueMemberS/...MemberN).
  • ReturnValues: types.ReturnValueAllNew — returns the full item after the update in out.Attributes (also ReturnValueUpdatedNew, ReturnValueAllOld, ReturnValueUpdatedOld, or ReturnValueNone).
  • Upsert semanticsUpdateItem creates the item if the key doesn't exist. Add a ConditionExpression (e.g. attribute_exists(Artist)) to update-only.
  • Prefer building expressions in code? The expression package (github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression) composes SET/ADD clauses 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.

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.