首页 > 解决方案 > 如何使用 aws sdk go v2 初始化 PutItemInput 结构以在 DynamoDb 上执行 PutItem 而不会出现此错误?

问题描述

当我尝试使用aws go sdk v2初始化输入变量以在 DynamoDB 表上执行 PutItem 时,它给了我一个错误 - 在复合文字中缺少类型。我在这里查看了结构 PutItemInput - https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/dynamodb@v1.2.2#PutItemInput我不明白如何摆脱这个错误?id 是 mytable 中的分区键

input := &dynamodb.PutItemInput{
            TableName: aws.String("mytable"),
            Item: map[string]types.AttributeValue{
                "id": {
                    N: aws.String(a.ID),
                },
            },
        }

标签: goaws-sdk-go

解决方案


Item: map[string]types.AttributeValue{
    "id": &types.AttributeValueMemberN{a.ID},
}

types.AttributeValue is an interface type. When constructing maps or slices of interfaces you cannot elide the concrete type of the individual elements from the composite literal. You need to tell the compiler what concrete type you would like an element to have.


推荐阅读