Golang DynamoDB UnmarshalListOfMaps 返回空数组

我有一个 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)

我在这里做错了什么?


侃侃尔雅
浏览 124回答 1
1回答

忽然笑

只有公共字段可以解组。使用大写字母公开您的结构字段,并使用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"`}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go