首页 文章

Golang DynamoDB UnmarshalListOfMaps返回Empty数组

提问于
浏览
1

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

[{},{}]

结构:

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

unmarshal的代码在这里:

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 回答

  • 1

    只有公共字段可以解组 .

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

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

相关问题