-2

golang と AWS を使用したプログラミングは初めてです。関数内のコード ブロック。AWS DynamoDB を使用して新しいテーブルを作成し、そこに値を書き込もうとしています。作成は成功しますが、書き込みが発生するとプログラムがクラッシュします。理由がわからない..誰かが私を助けてくれたら本当に感謝しています!

**Logs**:

2015/07/22 15:46:46 TableStatus: 0xc208193cb0
2015/07/22 15:46:46 End
2015/07/22 15:46:48 Sleep 2: Before Write
2015/07/22 15:46:48 Before Defining Input
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x401b28]

**Code Block:**
 {
    log.Println("Entry+++")
        cfg := aws.DefaultConfig

        svc := dynamodb.New(cfg)

        tableDefinition := &dynamodb.CreateTableInput{
            TableName:            aws.String("table1"),
            AttributeDefinitions: make([]*dynamodb.AttributeDefinition, 1, 1),
            KeySchema:            make([]*dynamodb.KeySchemaElement, 1, 1),
            ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
                ReadCapacityUnits:  aws.Long(1),
                WriteCapacityUnits: aws.Long(1),
            },
        }

        tableDefinition.KeySchema[0] = &dynamodb.KeySchemaElement{
            AttributeName: aws.String("batch_id"),
            KeyType:       aws.String("HASH"),
        }
        tableDefinition.AttributeDefinitions[0] = &dynamodb.AttributeDefinition{
            AttributeName: aws.String("batch_id"),
            AttributeType: aws.String("S"),
        }

        resp, err := svc.CreateTable(tableDefinition)
        log.Println("After CreateTable---")

        if err != nil {
            log.Println("create table failed", err.Error())
            return
        }
        if resp != nil && resp.TableDescription != nil {
            log.Println(
                "TableStatus:", resp.TableDescription.TableStatus)
        }

        log.Println("End")
        //Some time before the createTable transaction gets committed. 
        time.Sleep(2 * time.Second)
        log.Println("Sleep 2 Before Write")

        testA := "batch_1" //value to be written to the db
        // testB := "batch_name"
        // testC := "530"
        // testD := "Sample-Keyy-98"

        log.Println("Before Defining Input")
        input := &dynamodb.PutItemInput{
            TableName: aws.String("table1"),
            Item: map[string]*dynamodb.AttributeValue{
                "batch_id": &dynamodb.AttributeValue{
                    S: aws.String(testA),
                },
                // "name": &dynamodb.AttributeValue{
                //  S: aws.String(testB),
                // },
            },
        }

        _, err2 := svc.PutItem(input)
    }
4

2 に答える 2