gpt4 book ai didi

amazon-web-services - Golang DynamoDB UnmarshalListOfMaps 返回空数组

转载 作者:IT王子 更新时间:2023-10-29 02:01:08 25 4
gpt4 key购买 nike

我有一个 DynamoDB 产品表(id (int), active (bool), name (string), price (int)),当我检索并尝试解码列表时,它返回空。

[{},{}]

结构:

type Product struct {
id int
active bool
name string
price int }

解码代码在这里:

    params := &dynamodb.ScanInput{
TableName: aws.String("Products"),
}
result, err := service.Scan(params)
if err != nil {
fmt.Errorf("failed to make Query API call, %v", err)
}

var products = []Product{}

var error = dynamodbattribute.UnmarshalListOfMaps(result.Items, &products)

我在这里做错了什么?

最佳答案

只能解码公共(public)字段。

使用大写字母公开您的结构字段,并使用 json 属性将它们映射到数据值:

type Product struct {
ID int `json:"id"`
Active bool `json:"active"`
Name string `json:"name"`
Price int `json:"price"`
}

2021 年 10 月更新:

AWS SDK v1 使用 json 属性进行 DynamoDB 序列化。新版本 aws-sdk-go-v2包含重大更改并从 json 移动到 dynamodbav 属性以分隔 JSON 和 DynamoDB 名称。

对于 V2 结构应该是这样的:

type Product struct {
ID int `dynamodbav:"id"`
Active bool `dynamodbav:"active"`
Name string `dynamodbav:"name"`
Price int `dynamodbav:"price"`
}

文档:https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/dynamodbattribute/#Marshal

关于amazon-web-services - Golang DynamoDB UnmarshalListOfMaps 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com