go - How to work with context global variables in Golang?
问题描述
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()
}
解决方案
您收到该错误是因为您从未将任何内容分配给包级别ctx
变量,因此它仍然是nil
.
在内部init()
使用创建局部变量的短变量声明:
ctx := context.Background()
如果您更改为 simple assignment,它将为现有的包级ctx
变量分配一个值:
ctx = context.Background()
尽管使用“全局”变量来存储非全局变量是不好的做法。你应该只是通过ctx
它需要的地方。
推荐阅读
- json - json和soap API使用什么应用服务器
- django - 如何通过从 django 中的数据库中检索选项来在下拉菜单中显示类别?
- python - pygsheet 操作超时 [Errno 60]
- c# - 使用 FiddlerCore 从特定 URL 捕获响应
- ios - Animate UICollectionView contentInset adjustment
- javascript - 在挂载时执行的第一个 GET 请求期间加载微调器不起作用
- reactjs - 在文件之间传递状态值
- javascript - vue默认重定向导致最大堆栈错误
- c# - 从 EF Core 上下文中获取类型
- python - 删除两个特定行号之间的所有行