【发布时间】:2021-10-22 15:48:40
【问题描述】:
我收到以下错误:cannot use dynamodbattribute.UnmarshalMap(result.Item, &item) (type error) as type Item in assignment 当我尝试使用 UnmarshalMap 方法分配我的结构时。
我从此处的 DynamoDB 表中获取项目
result, err := svc.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{
"ForumName": {
S: aws.String(forumName),
},
},
})
并在获取以下 JSON 响应正文
{
"Item":{
"Threads":{
"N":"30"
},
"Category":{
"S":"Amazon Web Services"
},
"Messages":{
"N":"11"
},
"Views":{
"N":"99"
},
"ForumName":{
"S":"AWS Step Functions"
}
}
}
现在当我尝试分配我的结构时
type Item struct {
ForumName string `dynamodbav:"ForumName,stringset"`
Category string `dynamodbav:"Category,stringset"`
Messages int `dynamodbav:"Messages,numberset"`
Threads int `dynamodbav:"Threads,numberset"`
Views int `dynamodbav:"Views,numberset"`
}
var item Item
item = dynamodbattribute.UnmarshalMap(result.Item, &item)
我在这里收到错误item = dynamodbattribute.UnmarshalMap(result.Item, &item)
我也尝试过这样设置我的结构
type Item struct {
ForumName string
Category string
Messages int
Threads int
Views int
}
【问题讨论】:
-
如错误所示,UnmarshalMap 返回一个
error类型的值。将返回值分配给error类型的变量,并根据需要处理错误。示例:err := dynamodbattribute.UnmarshalMap(result.Item, &item); if err != nil { log.Fatal(err) }。该值被解组为&item。
标签: amazon-web-services go amazon-dynamodb aws-sdk-go