How do I initialize PutItemInput struct using aws sdk go v2 to execute a PutItem on DynamoDb without this error?

Issue

When I try to initialize the input variable to do a PutItem on DynamoDB table using aws go sdk v2, it gives me an error – missing type in composite literal. I looked at the struct PutItemInput here – https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/dynamodb@v1.2.2#PutItemInput I don’t understand how to get rid of this error? id is the partition key in mytable

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

Solution

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.

Answered By – mkopriva

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.