2

I have the following struct:

type User struct {
  ID       string    `json:"id"`
  Name     string    `json:"name"`
  LastName string    `json:"lastName"`
  User     string    `json:"user"`
  Password string    `json:"password"`
  Vehicles []Vehicle `json:"vehicles"`
}
type Vehicle struct {
  Plate string `json:"plate"`
}

I want to store an array of Vehicles in my DynamoDB. I did some research and I found that I should use the following code:

input := &dynamodb.PutItemInput{
    TableName: aws.String(tableUsers),
    Item: map[string]*dynamodb.AttributeValue{
        "id": {
            S: aws.String(fmt.Sprintf("%v", uuid)),
        },
        "name": {
            S: aws.String(user.Name),
        },
        "lastName": {
            S: aws.String(user.LastName),
        },
        "user": {
            S: aws.String(user.User),
        },
        "password": {
            S: aws.String(user.Password),
        },
        "vehicles": {
            L: [{
                M: {
                    "plate": {S: aws.String("test")},
                },
            }],
        },
    },
}

But I keep having a syntax error in:

L: [{
    M: {
        "plate": {S: aws.String("test")},
    },
}],

What am I doing wrong?

1 Answer 1

6

If you look at the dynamodb's godoc: https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#AttributeValue

You can see that the field L has the following type: []*AttributeValue

When you create a slice litteral you should specify its type. So for you case it's:

L: []*dynamodb.AttributeValue{
  {
    M: map[string]*dynamodb.AttributeValue{
      "plate": {S: aws.String("test")}
    }
  }
}

If you want to better understand struct, slices and map your can read the following articles:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.