【问题标题】:Golang aws-sdk-go dynamodb UnmarshalMap type error assignmentGolang aws-sdk-go dynamodb UnmarshalMap 类型错误赋值
【发布时间】:2021-10-22 15:48:40
【问题描述】:

我收到以下错误:cannot use dynamodbattribute.UnmarshalMap(result.Item, &item) (type error) as type Item in assignment 当我尝试使用 UnmarshalMap 方法分配我的结构时。

我从此处的 DynamoDB 表中获取项目

result, err := svc.GetItem(&dynamodb.GetItemInput{
        TableName: aws.String(tableName),
        Key: map[string]*dynamodb.AttributeValue{
            "ForumName": {
                S: aws.String(forumName),
            },
        },
    })

并在获取以下 JSON 响应正文

{
   "Item":{
      "Threads":{
         "N":"30"
      },
      "Category":{
         "S":"Amazon Web Services"
      },
      "Messages":{
         "N":"11"
      },
      "Views":{
         "N":"99"
      },
      "ForumName":{
         "S":"AWS Step Functions"
      }
   }
}

现在当我尝试分配我的结构时

    type Item struct {
        ForumName string `dynamodbav:"ForumName,stringset"`
        Category  string `dynamodbav:"Category,stringset"`
        Messages  int    `dynamodbav:"Messages,numberset"`
        Threads   int    `dynamodbav:"Threads,numberset"`
        Views     int    `dynamodbav:"Views,numberset"`
    }

    var item Item

    item = dynamodbattribute.UnmarshalMap(result.Item, &item)

我在这里收到错误item = dynamodbattribute.UnmarshalMap(result.Item, &item)

我也尝试过这样设置我的结构

    type Item struct {
        ForumName string 
        Category  string 
        Messages  int    
        Threads   int    
        Views     int    
    }

【问题讨论】:

  • 如错误所示,UnmarshalMap 返回一个 error 类型的值。将返回值分配给error 类型的变量,并根据需要处理错误。示例:err := dynamodbattribute.UnmarshalMap(result.Item, &item); if err != nil { log.Fatal(err) }。该值被解组为&item

标签: amazon-web-services go amazon-dynamodb aws-sdk-go


【解决方案1】:

尝试像 Cerise Limón 所说的那样正确管理错误,因为您将来自 dynamodbattribute.UnmarshalMap 的类型 error 放入预定义的 Item 类型变量中。所以它不会编译。

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 2022-11-23
    • 2019-06-04
    • 2017-03-15
    • 2014-08-04
    • 2017-02-22
    • 2022-11-30
    • 2021-03-05
    • 1970-01-01
    相关资源
    最近更新 更多