英文:
panic runtime error while fetching records from DynamoDB using golang
问题
我在使用aws-sdk-go访问时遇到了问题,我正在尝试使用aws-sdk-go连接到DynamoDB。我能够建立与数据库的连接,并且插入操作正常工作,但是当我尝试从表中检索数据时,我得到了"panic runtime error invalid memory address or nil pointer dereference 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: runtime error: invalid memory address or nil pointer dereference
goroutine 18 [running]:
net/http.(*conn).serve.func1(0xc820102000)
/usr/local/go/src/net/http/server.go:1389 +0xc1
panic(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
net/http.(*conn).serve(0xc820102000)
/usr/local/go/src/net/http/server.go:1472 +0xf2e
created by 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: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc82006e200)
/usr/local/go/src/net/http/server.go:1389 +0xc1
panic(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
net/http.(*conn).serve(0xc82006e200)
/usr/local/go/src/net/http/server.go:1472 +0xf2e
created by 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: runtime error: invalid memory address or nil pointer dereference
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc820114280)
/usr/local/go/src/net/http/server.go:1389 +0xc1
panic(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
net/http.(*conn).serve(0xc820114280)
/usr/local/go/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2137 +0x44e
英文:
I am facing issue while accessing aws-sdk-go, I am trying to connect to dynamo DB using aws-sdk-go. I am able to establish connection to database and insert operation is working fine, but when I am trying to retrieve data from table I am getting "panic runtime error invalid memory address or nil pointer dereference golang" error. Please find code I used. I am new to both technologies.
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
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)
}
Full Error I am getting
> http: panic serving 127.0.0.1:36034: runtime error: invalid memory address or nil pointer dereference
goroutine 18 [running]:
net/http.(*conn).serve.func1(0xc820102000)
/usr/local/go/src/net/http/server.go:1389 +0xc1
panic(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
net/http.(*conn).serve(0xc820102000)
/usr/local/go/src/net/http/server.go:1472 +0xf2e
created by 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: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc82006e200)
/usr/local/go/src/net/http/server.go:1389 +0xc1
panic(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
net/http.(*conn).serve(0xc82006e200)
/usr/local/go/src/net/http/server.go:1472 +0xf2e
created by 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: runtime error: invalid memory address or nil pointer dereference
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc820114280)
/usr/local/go/src/net/http/server.go:1389 +0xc1
panic(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
net/http.(*conn).serve(0xc820114280)
/usr/local/go/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2137 +0x44e
答案1
得分: 2
我已经查看了你的代码,我觉得你的数据库连接有问题。你可能正在尝试连接到不同的区域。所以,请先检查一下你的区域。它在你的dynamodb的AWS账户的URL中(例如us-east-2)。你也可以通过在终端中输入以下命令来检查或配置你的区域:aws configure。然后它会要求你输入访问密钥、秘密密钥、区域名称等详细信息。在这里输入所有的细节。就这样。干杯。
英文:
I have gone through your code and it seems to me that, there is problem with your database connectivity. You are trying to connect to maybe some different region. So, please first recheck your region. It is there in the URL of AWS account of your dynamodb.(e.g us-east-2). You can also check or configure your region by typing this command into the terminal : aws configure . Then it will ask you to enter your access key, secret key, region name etc .Enter all the details here. That's it. Cheers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论