How to retrieve nested map values
Question: I want to scan AWS DynamoDB table and then pull only a certain value. Here is
my code:

    
    
    package main
    
    import (
        "fmt"
    
        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/dynamodb"
    )
    
    func main() {
        svc := dynamodb.New(session.New(), &aws.Config{Region: aws.String("us-west-2")})
    
        params := &dynamodb.ScanInput{
            TableName: aws.String("my_Dynamo_table_name"),
            Limit: aws.Int64(2),
        }
    
        resp, err := svc.Scan(params)
        if err != nil {
            fmt.Println(err.Error())
            return
        }
    
        fmt.Println(resp)
    }
    

and The output is:

    
    
    {
      Count: 2,
      Items: [{
          update_time: {
            N: "1466495096"
          },
          create_time: {
            N: "1465655549"
          }
        },{
          update_time: {
            N: "1466503947"
          },
          create_time: {
            N: "1466503947"
          }
        }],
      LastEvaluatedKey: {
        Prim_key: {
          S: "1234567890"
        }
      },
      ScannedCount: 2
    }
    

Now, I want to retrieve the `update_time` value for all elements in above
output. Here are my attempts:

    
    
    for _, value := range resp.Items {
        fmt.Println(value["create_time"]["N"])
    }
    

and

    
    
    for _, value := range resp.Items {
        fmt.Println(value.create_time.N)
    }
    

and

    
    
    for _, value := range resp.Items {
        fmt.Println(*value.create_time.N)
    }
    

All above attempts error out with `/var/tmp/dynamo.go:37: invalid operation:`
error. I am from perl/python background and recently started learning golang.

How to retrieve nested map/array values in this case. Also, any reading
references would be of great help. My google search did not reveal anything
relevant.


Answer: The value of `resp` above is of the type `*ScanOutput`, which has `Items` type
as `[]map[string]*AttributeValue`.

To access `update_time`, you can try:

    
    
    updateTimes := make([]string, 0)
    
    // Items is a slice of map of type map[string]*AttributeValue
    for _, m := range resp.Items {
        // m is of type map[string]*AttributeValue
        timeStrPtr := *m["update_time"].N
        updateTimes = append(updateTimes, *timeStrPtr)
    }
    

`updateTimes` should now contains all the `"update_time"` values as strings.

More details [here](https://godoc.org/github.com/aws/aws-sdk-
go/service/dynamodb#ScanOutput).

