Cant Unmarshall dynamodb属性 [英] Cant unmarshall dynamodb attribute
问题描述
我正在使用带有DynamoDB API的AWS-SDK-GO进行实验...
I'm experimenting with AWS-SDK-GO with the DynamoDB API...
我正在尝试查询数据库并返回一个字符串.但是我在解决返回值问题上遇到了一些问题....
I'm trying to query the db and return a string. But I'm having some issues unmarshelling the return value....
结构
type Item struct {
slug string
destination string
}
查询功能
input := &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"slug": {
S: aws.String(slug),
},
},
TableName: db.TableName,
}
result, err := db.dynamo.GetItem(input)
if err != nil {
return "", err
}
n := Item{}
err = dynamodbattribute.UnmarshalMap(result.Item, &n)
if err != nil {
log.Printf("Failed to unmarshal Record, %v", err)
return "", err
}
log.Printf("dump %+v", n)
log.Printf("echo %s", n.slug)
log.Printf("echo %s", n.destination)
log.Printf("orig %v", result.Item)
结果
2017/10/11 14:21:34 dump {slug: destination:}
2017/10/11 14:21:34 echo
2017/10/11 14:21:34 echo
2017/10/11 14:21:34 orig map[destination:{
S: "http://example.com"
} slug:{
S: "abcde"
}]
为什么将商品退回为空?
Why is Item being returned empty?
我尝试到处寻找,但找不到解决方法....
I've tried to look everywhere but find no solution....
推荐答案
我不确定您是否尝试过此方法.我认为,如果您可以按以下所述更改结构,则可以解决问题.
I am not sure whether you have tried this. I think if you can change the struct as mentioned below, it may resolve the problem.
我假设 slug
和 destination
都已定义/保存为DynamoDB表中的String属性.
I assumed that both slug
and destination
are defined/saved as String attribute in DynamoDB table.
type Item struct {
Slug string`json:"slug"`
Destination string`json:"destination"`
}
将打印内容更改为:-
log.Printf("echo %s", n.Slug)
log.Printf("echo %s", n.Destination)
这篇关于Cant Unmarshall dynamodb属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!