使用 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)
}