AWS SDK for Go
Developer Guide

Reading an Amazon DynamoDB Table Item

The following example uses the DynamoDBGetItem operation to retrieve information about 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_read_item.go. Add the following statements to import the Go and AWS SDK for Go packages used in the example.

import ( "fmt" "" "" "" ""

Create the data structures we use to contain the information about the table item.

type ItemInfo struct { Plot string`json:"plot"` Rating float64`json:"rating"` } type Item struct { Year int`json:"year"` Title string`json:"title"` Info ItemInfo`json:"info"` }

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

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

Call GetItem to get the item from the table. If we encounter an error, print the error message. Otherwise, display information about the item.

result, err := svc.GetItem(&dynamodb.GetItemInput{ TableName: aws.String("Movies"), Key: map[string]*dynamodb.AttributeValue{ "year": { N: aws.String("2015"), }, "title": { S: aws.String("The Big New Movie"), }, }, }) if err != nil { fmt.Println(err.Error()) return } item := Item{} err = dynamodbattribute.UnmarshalMap(result.Item, &item) if err != nil { panic(fmt.Sprintf("Failed to unmarshal Record, %v", err)) } if item.Title == "" { fmt.Println("Could not find 'The Big New Movie' (2015)") return } fmt.Println("Found item:") fmt.Println("Year: ", item.Year) fmt.Println("Title: ", item.Title) fmt.Println("Plot: ", item.Info.Plot) fmt.Println("Rating:", item.Info.Rating)

See the complete example on GitHub.