Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/session"
  6. "github.com/aws/aws-sdk-go/service/dynamodb"
  7. "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
  8.  
  9. "fmt"
  10. )
  11.  
  12. type Item struct {
  13. Year int
  14. Title string
  15. Plot string
  16. Rating float64
  17. }
  18.  
  19. func main() {
  20. sess := session.Must(session.NewSessionWithOptions(session.Options{
  21. SharedConfigState: session.SharedConfigEnable,
  22. }))
  23.  
  24. // Create DynamoDB client
  25. svc := dynamodb.New(sess)
  26. tableName := "Movies"
  27. movieName := "The Big New Movie"
  28. movieYear := "2015"
  29.  
  30. result, err := svc.GetItem(&dynamodb.GetItemInput{
  31. TableName: aws.String(tableName),
  32. Key: map[string]*dynamodb.AttributeValue{
  33. "Year": {
  34. N: aws.String(movieYear),
  35. },
  36. "Title": {
  37. S: aws.String(movieName),
  38. },
  39. },
  40. })
  41. if err != nil {
  42. fmt.Println(err.Error())
  43. return
  44. }
  45.  
  46. mydict := map[string]*dynamodb.AttributeValue{}
  47.  
  48. // @HERE you need to use the below struct to initialize the values that you care about.
  49. // notice the "&", its used to get a pointer from an AttributeValue. My suggestion would be
  50. // to explicitly set types where ever you can to reduce ambiguity
  51. mydict["Year"] = &dynamodb.AttributeValue{
  52. N: aws.String("2019"),
  53. }
  54. // snippet-end:[dynamodb.go.read_item.call]
  55.  
  56. // snippet-start:[dynamodb.go.read_item.unmarshall]
  57. item := Item{}
  58.  
  59. err = dynamodbattribute.UnmarshalMap(result.Item, &item)
  60. if err != nil {
  61. panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
  62. }
  63.  
  64. if item.Title == "" {
  65. fmt.Println("Could not find '" + movieName + "' (" + movieYear + ")")
  66. return
  67. }
  68.  
  69. fmt.Println("Found item:")
  70. fmt.Println("Year: ", item.Year)
  71. fmt.Println("Title: ", item.Title)
  72. fmt.Println("Plot: ", item.Plot)
  73. fmt.Println("Rating:", item.Rating)
  74. // snippet-end:[dynamodb.go.read_item.unmarshall]
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement