-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (129 loc) · 3.04 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
// Item is record for MyFirstTable.
type Item struct {
MyHashKey string `dynamodbav:"MyHashKey"`
MyRangeKey int `dynamodbav:"MyRangeKey"`
MyText string `dynamodbav:"MyText"`
}
func main() {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
db := dynamodb.New(sess, &aws.Config{Endpoint: aws.String("http://localhost:8000")})
put(db)
get(db)
update(db)
delete(db)
}
func put(db *dynamodb.DynamoDB) {
item := Item{
MyHashKey: "00001",
MyRangeKey: 1,
MyText: "some text...",
}
// Convert item to dynamodb attribute.
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
fmt.Println(err.Error())
return
}
// Create input.
input := &dynamodb.PutItemInput{
TableName: aws.String("MyFirstTable"),
Item: av,
}
// Execute.
_, err = db.PutItem(input)
if err != nil {
fmt.Println(err.Error())
return
}
}
func get(db *dynamodb.DynamoDB) {
// Create input.
input := &dynamodb.GetItemInput{
TableName: aws.String("MyFirstTable"),
Key: map[string]*dynamodb.AttributeValue{
"MyHashKey": {
S: aws.String("00001"),
},
"MyRangeKey": {
N: aws.String("1"),
},
},
}
// Execute.
result, err := db.GetItem(input)
if err != nil {
fmt.Println(err.Error())
return
}
// Convert the dynamodb result to a struct.
item := Item{}
err = dynamodbattribute.UnmarshalMap(result.Item, &item)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(item) // {00001 1 some text...}
}
func update(db *dynamodb.DynamoDB) {
// Create an expression for update.
update := expression.UpdateBuilder{}.Set(expression.Name("MyText"), expression.Value("updated text"))
expr, err := expression.NewBuilder().WithUpdate(update).Build()
if err != nil {
fmt.Println(err.Error())
return
}
// Create an input.
input := &dynamodb.UpdateItemInput{
TableName: aws.String("MyFirstTable"),
Key: map[string]*dynamodb.AttributeValue{
"MyHashKey": {
S: aws.String("00001"),
},
"MyRangeKey": {
N: aws.String("1"),
},
},
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
UpdateExpression: expr.Update(),
ConditionExpression: expr.Condition(),
ReturnValues: aws.String(dynamodb.ReturnValueAllNew),
}
// Execute.
_, err = db.UpdateItem(input)
if err != nil {
fmt.Println(err.Error())
return
}
}
func delete(db *dynamodb.DynamoDB) {
// Create an input.
input := &dynamodb.DeleteItemInput{
TableName: aws.String("MyFirstTable"),
Key: map[string]*dynamodb.AttributeValue{
"MyHashKey": {
S: aws.String("00001"),
},
"MyRangeKey": {
N: aws.String("1"),
},
},
}
// Execute.
_, err := db.DeleteItem(input)
if err != nil {
fmt.Println(err.Error())
return
}
}