为了账号安全,请及时绑定邮箱和手机立即绑定

Golang DynamoDB UnmarshalListOfMaps 返回空数组

Golang DynamoDB UnmarshalListOfMaps 返回空数组

Go
侃侃尔雅 2023-05-15 15:00:45
我有一个 DynamoDB 产品表(id (int)、active (bool)、name (string)、price (int)),当我检索并尝试解组列表时,它返回空。[{},{}]结构:type Product struct {id     intactive boolname   stringprice  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)我在这里做错了什么?
查看完整描述

1 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

只有公共字段可以解组。


使用大写字母公开您的结构字段,并使用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包含重大更改并从属性移动jsondynamodbav单独的 JSON 和 DynamoDB 名称。

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


type Product struct {

    ID     int    `dynamodbav:"id"`

    Active bool   `dynamodbav:"active"`

    Name   string `dynamodbav:"name"`

    Price  int    `dynamodbav:"price"`

}

查看完整回答
反对 回复 2023-05-15
  • 1 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信