I get a Panic if I attempt to run the tests or runthe app directly via: go run main.go The code works great on my old Macbook Pro. It panics on my new Macbook Pro.
I believe the error has Something to do with accessing the Firestore datastore but I'm not sure what's going wrong.
I'm running the same code so datastore Keys and everything else should be identical.
Here is the Github repo: https://github.com/golangnewb/FirestoreTestHttp
Working Computer: go version go1.11.2 darwin/amd64
Panic Computer: go version go1.13.5 darwin/amd64
❯ go test
--- FAIL: TestCities (0.01s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x14d557a]
goroutine 6 [running]:
testing.tRunner.func1(0xc000312200)
/usr/local/go/src/testing/testing.go:874 +0x3a3
panic(0x15ac100, 0x1b3e740)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
cloud.google.com/go/firestore.(*Client).path(...)
/Users/XXX/go/src/cloud.google.com/go/firestore/client.go:107
cloud.google.com/go/firestore.(*Client).Collection(0x0, 0x1677ac0, 0x6, 0x14)
/Users/XXX/go/src/cloud.google.com/go/firestore/client.go:122 +0x9a
_/Users/XXX/Dropbox/go/src/firestoreTestHttp.addCitiesFirestore(0x5e4c4feb, 0xc000064760)
/Users/XXX/Dropbox/go/src/firestoreTestHttp/city_handlers_test.go:218 +0x13d
_/Users/XXX/Dropbox/go/src/firestoreTestHttp.TestCities(0xc000312200)
/Users/XXX/Dropbox/go/src/firestoreTestHttp/city_handlers_test.go:19 +0x26
testing.tRunner(0xc000312200, 0x16a1c98)
/usr/local/go/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:960 +0x350
exit status 2
FAIL _/Users/XXX/Dropbox/go/src/firestoreTestHttp 0.165s
Here is the panic if I try to run via:
❯ go run main.go
2020/02/18 17:19:39 http: panic serving [::1]:60479: runtime error: invalid memory address or nil pointer dereference
goroutine 9 [running]:
net/http.(*conn).serve.func1(0xc00027c6e0)
/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x15a69c0, 0x1b39a50)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
cloud.google.com/go/firestore.(*Client).path(...)
/Users/XXX/go/src/cloud.google.com/go/firestore/client.go:107
cloud.google.com/go/firestore.(*Client).Collection(0x0, 0x16728ba, 0x6, 0x14)
/Users/XXX/go/src/cloud.google.com/go/firestore/client.go:122 +0x9a
main.indexHandler(0x1744360, 0xc000322000, 0xc0002c8200)
/Users/XXX/Dropbox/go/src/firestoreTestHttp/main.go:50 +0xa2
net/http.HandlerFunc.ServeHTTP(0x169da80, 0x1744360, 0xc000322000, 0xc0002c8200)
/usr/local/go/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xc0002ea100, 0x1744360, 0xc000322000, 0xc0002c8200)
/usr/local/go/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc000312000, 0x1744360, 0xc000322000, 0xc0002c8200)
/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc00027c6e0, 0x17457a0, 0xc00008cf80)
/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2928 +0x384
Although I dont feel well in Go I will try a shot. If I am wrong just let me know.
From the error it seems that Client is nil. In attached Git repo in main you are logging that it should be set as environment variable GCLOUD_PROJECT, however I haven't found any so.Getenv
function that gets this value. Maybe this value is wrong...?
So I guess this statement client, _ := firestore.NewClient(ctx, projectID)
is being called with wrong projectID. In the statement error handling is omitted, so we do not know if the client has been created properly. If it was not created, that we should get such error.
Maybe you should add error handling like in example in Go Firestore Reference, to ensure that there is no error during client creation.
I hope this will help you!