DynamoDBReadItem.go - AWS Code Sample

DynamoDBReadItem.go

DynamoDBReadItem.go gets an item from an Amazon DynamoDB table.

/* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" "fmt" ) // Create struct to hold info about new item type Item struct { Year int Title string Plot string Rating float64 } func main() { // Initialize a session that the SDK will use to load // credentials from the shared credentials file ~/.aws/credentials // and region from the shared configuration file ~/.aws/config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create DynamoDB client svc := dynamodb.New(sess) tableName := "Movies" movieName := "The Big New Movie" movieYear := "2015" result, err := svc.GetItem(&dynamodb.GetItemInput{ TableName: aws.String(tableName), Key: map[string]*dynamodb.AttributeValue{ "Year": { N: aws.String(movieYear), }, "Title": { S: aws.String(movieName), }, }, }) if err != nil { fmt.Println(err.Error()) return } if result.Item == nil { msg := "Could not find '" + *title + "'" return nil, errors.New(msg) } item := Item{} err = dynamodbattribute.UnmarshalMap(result.Item, &item) if err != nil { panic(fmt.Sprintf("Failed to unmarshal Record, %v", err)) } fmt.Println("Found item:") fmt.Println("Year: ", item.Year) fmt.Println("Title: ", item.Title) fmt.Println("Plot: ", item.Plot) fmt.Println("Rating:", item.Rating) }

Sample Details

Service: dynamodb

Last tested: 2019-03-19

Author: Doug-AWS

Type: full-example