I am trying to get all documents from a Firestore database and things were working fine.
But then I decided to make the context and client variable global, so that I won't have to deal with passing them as parameters everytime.
Things broke after that.
The error I get is:
panic: runtime error: invalid memory address or nil pointer dereference
and according to the stack trace, it occurs when I try to:
client.Collection("dummy").Documents(ctx)
What can I do to resolve this?
And how can I efficiently work with global variables in my case?
My code for reference:
package main
import (
"context"
"fmt"
"log"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
var (
ctx context.Context
client *firestore.Client
)
func init() {
ctx := context.Background()
keyFile := option.WithCredentialsFile("serviceAccountKey.json")
app, err := firebase.NewApp(ctx, nil, keyFile)
if err != nil {
log.Fatalln(err)
}
client, err = app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Connection to Firebase Established!")
}
func getDocuments(collectionName string) {
iter := client.Collection("dummy").Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
fmt.Println(doc.Data()["question"])
}
}
func main() {
getDocuments("dummy")
defer client.Close()
}