panic: runtime error: invalid memory address or nil pointer dereference (time and again)

panic: runtime error: invalid memory address or nil pointer dereference (time and again)

我对 golang 很陌生,大部分时间都不知道我到目前为止在做什么。我尝试 运行 一个简单的 find() 查询来从我的数据库中获取所有文档,但我似乎无法让它工作我不断收到此错误

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x58 pc=0x9e2623]

goroutine 40 [running]:
go.mongodb.org/mongo-driver/mongo.(*Collection).Find(0x0, 0xf9f2a0, 0xc0000a4008, 0xc07dc0, 0xc00034e1b0, 0x0, 0x0, 0x0, 0x42eda1, 0xcf4758, ...)
    /home/rashad/go/pkg/mod/go.mongodb.org/mongo-driver@v1.5.3/mongo/collection.go:1106 +0x63
github.com/RashadArbab/goServer/Routes.GetAll(0xc000432000, 0x0, 0x0)
    /home/rashad/goServer/Routes/handler.go:22 +0xf1
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0x60b8d06b, 0x1da784f8, 0x9bd981defeb4)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc000432000, 0x1feb2696b, 0x14d7d20)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/ctx.go:656 +0x88
github.com/gofiber/fiber/v2/middleware/logger.New.func2(0xc000432000, 0xc000342208, 0x4)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/middleware/logger/logger.go:155 +0x13f
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0x0, 0xc0003221e0, 0x14f6240)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc000432000, 0x0, 0x0)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/ctx.go:656 +0x88
github.com/gofiber/fiber/v2.(*App).registerStatic.func3(0xc000432000, 0xc000342208, 0x4)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:380 +0x180
github.com/gofiber/fiber/v2.(*App).next(0xc0003ea000, 0xc000432000, 0xc000432000, 0x1da63ae5, 0x1da63ae500000004)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:127 +0x1b0
github.com/gofiber/fiber/v2.(*App).handler(0xc0003ea000, 0xc000424000)
    /home/rashad/go/pkg/mod/github.com/gofiber/fiber/v2@v2.11.0/router.go:155 +0x118
github.com/valyala/fasthttp.(*Server).serveConn(0xc0003ec000, 0xfa5940, 0xc00032a008, 0x0, 0x0)
    /home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/server.go:2219 +0x1497
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0003ee140, 0xc000322140)
    /home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:223 +0xc0
github.com/valyala/fasthttp.(*workerPool).getCh.func1(0xc0003ee140, 0xc000322140, 0xba7ae0, 0xc000322140)
    /home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:195 +0x35
created by github.com/valyala/fasthttp.(*workerPool).getCh
    /home/rashad/go/pkg/mod/github.com/valyala/fasthttp@v1.26.0/workerpool.go:194 +0x101

我通过了检查点 1 并在检查点 2 之前卡住了。一切似乎都正常,我可以连接到数据库并 ping 它以确保连接成功,但是当我从中获取一些数据时它引发恐慌。

这里有一些相关的代码

(我正在尝试 ping handler.go 中的 GetAll() 函数)

handler.go

package Routes

import (
    "context"
    "encoding/json"
    "fmt"
    "log"

    "github.com/RashadArbab/goServer/Database"
    "github.com/gofiber/fiber/v2"
    "go.mongodb.org/mongo-driver/bson"
)

var DB = Database.DB
var ctx = context.TODO()

func GetAll(c *fiber.Ctx) error {

    fmt.Println("Checkpoint 1")
    var users []Database.User

    

    cursor, err := DB.Find(ctx, bson.M{})
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Checkpoint 2")

    // Close the cursor once finished
    /*A defer statement defers the execution of a function until the surrounding function returns.
    simply, run cur.Close() process but after cur.Next() finished.*/

    defer cursor.Close(context.TODO())
    fmt.Println("Checkpoint 3")

    for cursor.Next(context.TODO()) {

        // create a value into which the single document can be decoded
        var user Database.User
        // & character returns the memory address of the following variable.
        err := cursor.Decode(&user) // decode similar to deserialize process.
        if err != nil {
            log.Fatal(err)
        }

        // add item our array
        users = append(users, user)
    }

    fmt.Println("Checkpoint 4")

    documents, _ := json.Marshal(users)
    return c.JSON(documents)

}

func CreateProd(c *fiber.Ctx) error {
    return c.SendString("This is create product")
}

func GetSingle(c *fiber.Ctx) error {
    id := c.Params("id")
    return c.SendString("this is create singular" + id)
}

main.go

package main

import (
    "github.com/RashadArbab/goServer/Database"
    "github.com/RashadArbab/goServer/Routes"

    "github.com/gofiber/fiber/v2"
)

func main() {
    Database.Init()

    app := fiber.New()

    app.Static("/", "./AA-Frontend/build")

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("hello there")
    })

    Routes.SetupRoutes(app)

    app.Listen(":5000")
}

database.go

package Database

import (
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"

    "context"
    "fmt"
    "log"
)

var DB *mongo.Collection
var ctx = context.TODO()

func Init() *mongo.Collection {
    clientOptions := options.Client().ApplyURI("mongodb://localhost:27017/")
    client, err := mongo.Connect(ctx, clientOptions)
    if err != nil {
        log.Fatal(err)
    }
    err = client.Ping(ctx, nil)
    if err != nil {
        log.Fatal(err)
    } else {
        fmt.Println()
    }

    DB = client.Database("GoServer").Collection("users")
    fmt.Print(DB.Name())

    return DB
}

DB 为零,因为 var DB=database.DBdatabase.DB 初始化之前运行。

直接使用database.DB