forked from awsdocs/aws-doc-sdk-examples
/
DeleteItem.go
81 lines (70 loc) · 2.53 KB
/
DeleteItem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[dynamodb.go.delete_table_item]
package main
// snippet-start:[dynamodb.go.delete_table_item.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
// snippet-end:[dynamodb.go.delete_table_item.imports]
// DeleteTableItem deletes an item from a table
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// table is the name of the table
// movie is the name of the movie
// year is when the movie was released
// Output:
// If success, nil
// Otherwise, an error from the call to DeleteItem
func DeleteTableItem(svc dynamodbiface.DynamoDBAPI, table, movie, year *string) error {
// snippet-start:[dynamodb.go.delete_table_item.call]
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"Year": {
N: year,
},
"Title": {
S: movie,
},
},
TableName: table,
}
_, err := svc.DeleteItem(input)
// snippet-end:[dynamodb.go.delete_table_item.call]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[dynamodb.go.delete_table_item.args]
table := flag.String("t", "", "The name of the table")
movie := flag.String("m", "", "The name of the movie")
year := flag.String("y", "", "The year the movie was released")
flag.Parse()
if *table == "" || *movie == "" || *year == "" {
fmt.Println("You must specify a table, movie title, and year the movie was released:")
fmt.Println("-t TABLE -m MOVIE -y YEAR")
return
}
// snippet-end:[dynamodb.go.delete_table_item.args]
// snippet-start:[dynamodb.go.delete_table_item.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create DynamoDB client
svc := dynamodb.New(sess)
// snippet-end:[dynamodb.go.delete_table_item.session]
err := DeleteTableItem(svc, table, movie, year)
if err != nil {
fmt.Println("Got an error deleting movie " + *movie + " from table " + *table + ":")
fmt.Println(err)
return
}
fmt.Println("Deleted '" + *movie + " from table " + *table)
}
// snippet-end:[dynamodb.go.delete_table_item]