Menu
AWS SDK for Go
Developer Guide

Updating an Amazon DynamoDB Table Item

The following example uses the DynamoDBUpdateItem operation to update the rating to 0.5 for the item with the year 2015 and title The Big New Movie in the Movies table in the us-west-2 region.

Create the file dynamodb_update_item.go. Add the following statements to import the Go and AWS SDK for Go packages used in the example.

Copy
import ( "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" )

Initialize the session that the SDK uses to load credentials from the shared credentials file ~/.aws/credentials, and create a new DynamoDB service client.

Copy
sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create DynamoDB client svc := dynamodb.New(sess)

Call UpdateItem to add the item to the table. If we encounter an error, print the error message. Otherwise, display a message that the item was updated.

Copy
input := &dynamodb.UpdateItemInput{ ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ ":r": { N: aws.String("0.5"), }, }, TableName: aws.String("Movies"), Key: map[string]*dynamodb.AttributeValue{ "year": { N: aws.String("2015"), }, "title": { S: aws.String("The Big New Movie"), }, }, ReturnValues: aws.String("UPDATED_NEW"), UpdateExpression: aws.String("set info.rating = :r"), } _, err = svc.UpdateItem(input) if err != nil { fmt.Println(err.Error()) return } fmt.Println("Successfully updated 'The Big New Movie' (2015) rating to 0.5")

See the complete example on GitHub.