AWS SDK for Go
Developer Guide

Creating an Amazon DynamoDB Table Item Using the AWS SDK for Go

The following example uses the DynamoDBPutItem operation to create the table 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_create_item.go. Add the following statements to import the Go and AWS SDK for Go packages used in the example.

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

Create the data structures we use to containing 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)

Initialize the structs with the movie data and marshall that data into a map of AttributeValue objects.

info := ItemInfo{ Plot: "Nothing happens at all.", Rating: 0.0, } item := Item{ Year: 2015, Title: "The Big New Movie", Info: info, } av, err := dynamodbattribute.MarshalMap(item)

Create the input for PutItem and call it. If an error occurs, print the error and exit. If no error occurs, print an message that the item was added to the table.

input := &dynamodb.PutItemInput{ Item: av, TableName: aws.String("Movies"), } _, err = svc.PutItem(input) if err != nil { fmt.Println("Got error calling PutItem:") fmt.Println(err.Error()) os.Exit(1) } fmt.Println("Successfully added 'The Big New Movie' (2015) to Movies table")

See the complete example on GitHub.