我有一個 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包含重大更改并從屬性移動json
到dynamodbav
單獨的 JSON 和 DynamoDB 名稱。
對于 V2 結構應該是這樣的:
type Product struct {
? ? ID? ? ?int? ? `dynamodbav:"id"`
? ? Active bool? ?`dynamodbav:"active"`
? ? Name? ?string `dynamodbav:"name"`
? ? Price? int? ? `dynamodbav:"price"`
}
- 1 回答
- 0 關注
- 150 瀏覽
添加回答
舉報
0/150
提交
取消