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.

package main import ( "fmt" "os" "github.com/aws/aws-sdk-go/aws"

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

) // Create structs to hold info about new item type ItemInfo struct { Plot string`json:"plot"` Rating float64`json:"rating"` } type Item struct { Year int`json:"year"`

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

main() { // Initialize a session in us-west-2 that the SDK will use to load // credentials from the shared credentials file ~/.aws/credentials. sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, )

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

// Create DynamoDB client svc := dynamodb.New(sess) info := ItemInfo{ Plot: "Nothing happens at all.", Rating: 0.0, } item := Item{ Year: 2015, Title: "The Big New Movie", Info: info,

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.

} // Create item in table Movies 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)

See the complete example on GitHub.