Created
January 23, 2018 07:17
-
-
Save doncicuto/be1437ce0eeefb58790b63c77b86472f to your computer and use it in GitHub Desktop.
DynamoDB Tutorial Part 3 (Create a new Item)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
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" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | |
) | |
func main() { | |
type MovieInfo struct { | |
Plot string `json:"plot"` | |
Rating float64 `json:"rating"` | |
} | |
type Movie struct { | |
Year int `json:"year"` | |
Title string `json:"title"` | |
Info MovieInfo `json:"info"` | |
} | |
movie := Movie{ | |
Year: 2015, | |
Title: "The Big New Movie", | |
Info: MovieInfo{ | |
Plot: "Nothing happens at all.", | |
Rating: 0.0, | |
}, | |
} | |
config := &aws.Config{ | |
Region: aws.String("us-west-2"), | |
Endpoint: aws.String("http://localhost:8000"), | |
} | |
sess := session.Must(session.NewSession(config)) | |
svc := dynamodb.New(sess) | |
av, err := dynamodbattribute.MarshalMap(movie) | |
input := &dynamodb.PutItemInput{ | |
Item: av, | |
TableName: aws.String("Movies"), | |
} | |
_, err = svc.PutItem(input) | |
if err != nil { | |
fmt.Println(err.Error()) | |
return | |
} | |
fmt.Printf("We have inserted a new item!\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment