亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號