我在访问aws-sdk-go时遇到问题,我正在尝试使用aws-sdk-go连接到dynamo DB。我能够建立与数据库的连接并且插入操作工作正常,但是当我尝试从表中检索数据时,我得到了#34;恐慌运行时错误无效的内存地址或者nil指针取消引用golang"错误。请查找我使用的代码。我是这两种技术的新手。
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/user/dynamo_connect/data"
"github.com/user/insert_api/read"
)
import (
"fmt"
"log"
"net/http"
"crypto/md5"
"encoding/hex"
"io"
"time"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/get", GetCampaignRecord)
log.Fatal(http.ListenAndServe(":8080", router))
}
func GetCampaignRecord(w http.ResponseWriter, r *http.Request) {
var testCredentials = credentials.NewStaticCredentials("AKIxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "")
sess, err := session.NewSession()
svc := dynamodb.New(sess, &aws.Config{
Region: aws.String("ap-south-1"),
Credentials: testCredentials,
})
resp, err := read.GetCampaignData(svc)
if err != nil {
fmt.Println("An error occurred while writing to the Employee table")
fmt.Println(err.Error())
}
if resp != nil {
fmt.Println("An error occurred while writing to the Employee table")
fmt.Println(err.Error())
}
}
包阅读
package read
import "github.com/aws/aws-sdk-go/service/dynamodb"
import "github.com/aws/aws-sdk-go/aws"
func GetCampaignData(svc *dynamodb.DynamoDB) (*dynamodb.BatchGetItemOutput, error) {
params := &dynamodb.BatchGetItemInput{
RequestItems: map[string]*dynamodb.KeysAndAttributes{
"employee": {
Keys: []map[string]*dynamodb.AttributeValue{
{
"employee_id": {
S: aws.String("1"),
},
},
},
ProjectionExpression: aws.String("employee_id, employee_name"),
},
},
ReturnConsumedCapacity: aws.String("TOTAL"),
}
return svc.BatchGetItem(params)
}
完全错误我正在
http:panic serving 127.0.0.1:36034:运行时错误:无效的内存地址或无指针取消引用 goroutine 18 [跑步]: 净/ HTTP(*康恩).serve.func1(0xc820102000) /usr/local/go/src/net/http/server.go:1389 + 0xc1 恐慌(0x8919a0,0xc82000a110) /usr/local/go/src/runtime/panic.go:443 + 0x4e9 github.com/aws/aws-sdk-go/aws/session.(*Session).Copy(0x0,0xc82012a020,0x1,0x1,0x0) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:365 + 0x2a github.com/aws/aws-sdk-go/aws/session.(*Session).ClientConfig(0x0,0x935410,0x8,0xc82012a020,0x1,0x1,0x0,0x0,0x0,0x0, ......) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:378 + 0x72 github.com/aws/aws-sdk-go/service/dynamodb.New(0x7f20477cd248,0x0,0xc82012a020,0x1,0x1,0xc820112260) /home/MyUser/work/src/github.com/aws/aws-sdk-go/service/dynamodb/service.go:153 + 0x78 main.GetCampaignRecord(0x7f20477cd058,0xc820122000,0xc82010e000) /home/MyUser/work/src/github.com/user/insert_api/insert_api.go:80 + 0x329 net / http.HandlerFunc.ServeHTTP(0xa07d38,0x7f20477cd058,0xc820122000,0xc82010e000) /usr/local/go/src/net/http/server.go:1618 + 0x3a github.com/gorilla/mux.(*Router).ServeHTTP(0xc8200125a0,0x7f20477cd058,0xc820122000,0xc82010e000) /home/MyUser/work/src/github.com/gorilla/mux/mux.go:114 + 0x2a8 net / http.serverHandler.ServeHTTP(0xc82006e180,0x7f20477cd058,0xc820122000,0xc82010e000) /usr/local/go/src/net/http/server.go:2081 + 0x19e 净/ HTTP(*康恩).serve(0xc820102000) /usr/local/go/src/net/http/server.go:1472 + 0xf2e 由net / http。(* Server).Serve创建 /usr/local/go/src/net/http/server.go:2137 + 0x44e 2016/11/19 13:31:34 http:panic serving 127.0.0.1:36035:运行时错误:无效的内存地址或nil指针取消引用 goroutine 5 [正在运行]: 净/ HTTP(*康恩).serve.func1(0xc82006e200) /usr/local/go/src/net/http/server.go:1389 + 0xc1 恐慌(0x8919a0,0xc82000a110) /usr/local/go/src/runtime/panic.go:443 + 0x4e9 github.com/aws/aws-sdk-go/aws/session.(*Session).Copy(0x0,0xc82012a038,0x1,0x1,0x0) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:365 + 0x2a github.com/aws/aws-sdk-go/aws/session.(*Session).ClientConfig(0x0,0x935410,0x8,0xc82012a038,0x1,0x1,0x0,0x0,0x0,0x0, ......) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:378 + 0x72 github.com/aws/aws-sdk-go/service/dynamodb.New(0x7f20477cd248,0x0,0xc82012a038,0x1,0x1,0xc820112540) /home/MyUser/work/src/github.com/aws/aws-sdk-go/service/dynamodb/service.go:153 + 0x78 main.GetCampaignRecord(0x7f20477cd058,0xc8201224e0,0xc8200d20e0) /home/MyUser/work/src/github.com/user/insert_api/insert_api.go:80 + 0x329 net / http.HandlerFunc.ServeHTTP(0xa07d38,0x7f20477cd058,0xc8201224e0,0xc8200d20e0) /usr/local/go/src/net/http/server.go:1618 + 0x3a github.com/gorilla/mux.(*Router).ServeHTTP(0xc8200125a0,0x7f20477cd058,0xc8201224e0,0xc8200d20e0) /home/MyUser/work/src/github.com/gorilla/mux/mux.go:114 + 0x2a8 net / http.serverHandler.ServeHTTP(0xc82006e180,0x7f20477cd058,0xc8201224e0,0xc8200d20e0) /usr/local/go/src/net/http/server.go:2081 + 0x19e 净/ HTTP(*康恩).serve(0xc82006e200) /usr/local/go/src/net/http/server.go:1472 + 0xf2e 由net / http。(* Server).Serve创建 /usr/local/go/src/net/http/server.go:2137 + 0x44e 2016/11/19 13:31:34 http:panic serving 127.0.0.1:36036:运行时错误:无效的内存地址或nil指针取消引用 goroutine 34 [跑步]: 净/ HTTP(*康恩).serve.func1(0xc820114280) /usr/local/go/src/net/http/server.go:1389 + 0xc1 恐慌(0x8919a0,0xc82000a110) /usr/local/go/src/runtime/panic.go:443 + 0x4e9 github.com/aws/aws-sdk-go/aws/session.(*Session).Copy(0x0,0xc82012a058,0x1,0x1,0x0) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:365 + 0x2a github.com/aws/aws-sdk-go/aws/session.(*Session).ClientConfig(0x0,0x935410,0x8,0xc82012a058,0x1,0x1,0x0,0x0,0x0,0x0, ......) /home/MyUser/work/src/github.com/aws/aws-sdk-go/aws/session/session.go:378 + 0x72 github.com/aws/aws-sdk-go/service/dynamodb.New(0x7f20477cd248,0x0,0xc82012a058,0x1,0x1,0xc820112840) /home/MyUser/work/src/github.com/aws/aws-sdk-go/service/dynamodb/service.go:153 + 0x78 main.GetCampaignRecord(0x7f20477cd058,0xc820122820,0xc82016c000) /home/MyUser/work/src/github.com/user/insert_api/insert_api.go:80 + 0x329 net / http.HandlerFunc.ServeHTTP(0xa07d38,0x7f20477cd058,0xc820122820,0xc82016c000) /usr/local/go/src/net/http/server.go:1618 + 0x3a github.com/gorilla/mux.(*Router).ServeHTTP(0xc8200125a0,0x7f20477cd058,0xc820122820,0xc82016c000) /home/MyUser/work/src/github.com/gorilla/mux/mux.go:114 + 0x2a8 net / http.serverHandler.ServeHTTP(0xc82006e180,0x7f20477cd058,0xc820122820,0xc82016c000) /usr/local/go/src/net/http/server.go:2081 + 0x19e 净/ HTTP(*康恩).serve(0xc820114280) /usr/local/go/src/net/http/server.go:1472 + 0xf2e 由net / http。(* Server).Serve创建 /usr/local/go/src/net/http/server.go:2137 + 0x44e
答案 0 :(得分:1)
我已经完成了您的代码,在我看来,您的数据库连接存在问题。您正在尝试连接到某个不同的区域。所以,请先重新检查你所在的地区。它存在于您的dynamodb的AWS账户的URL中(例如us-east-2)。您还可以通过在终端中键入以下命令来检查或配置您的区域:aws configure。然后它会要求您输入您的访问密钥,密钥,区域名称等。在此处输入所有详细信息。而已。干杯。