Ped*_*lho 1 go amazon-web-services amazon-dynamodb
我有以下结构:
type User struct {
ID string `json:"id"`
Name string `json:"name"`
LastName string `json:"lastName"`
User string `json:"user"`
Password string `json:"password"`
Vehicles []Vehicle `json:"vehicles"`
}
type Vehicle struct {
Plate string `json:"plate"`
}
Run Code Online (Sandbox Code Playgroud)
我想在我的 DynamoDB 中存储一系列 Vehicles。我做了一些研究,发现我应该使用以下代码:
input := &dynamodb.PutItemInput{
TableName: aws.String(tableUsers),
Item: map[string]*dynamodb.AttributeValue{
"id": {
S: aws.String(fmt.Sprintf("%v", uuid)),
},
"name": {
S: aws.String(user.Name),
},
"lastName": {
S: aws.String(user.LastName),
},
"user": {
S: aws.String(user.User),
},
"password": {
S: aws.String(user.Password),
},
"vehicles": {
L: [{
M: {
"plate": {S: aws.String("test")},
},
}],
},
},
}
Run Code Online (Sandbox Code Playgroud)
但我一直有语法错误:
L: [{
M: {
"plate": {S: aws.String("test")},
},
}],
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
如果您查看 dynamodb 的 godoc:https ://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#AttributeValue
可以看到该字段L的类型如下:[]*AttributeValue
当你创建一个 slice litteral 时,你应该指定它的类型。所以对于你的情况是:
L: []*dynamodb.AttributeValue{
{
M: map[string]*dynamodb.AttributeValue{
"plate": {S: aws.String("test")}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想更好地理解结构体、切片和映射,你可以阅读以下文章:
| 归档时间: |
|
| 查看次数: |
792 次 |
| 最近记录: |