title: panic: runtime error: invalid memory address or nil pointer dereference (time and again)
body: <p>I am very new to golang and for the most part have no idea what im doing so far. I tried to run a simple find() query to get all documents from my databse and I cant seem to get it to work i keep getting this error</p>
  <pre><code>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
  </code></pre>
  <p>I get past checkpoint 1 and get stuck before checkpoint 2. everything seems to work, I can connect to the database and ping it to make sure the connection is successful, but then when i go to get some data from it it throws a panic.</p>
  <p>here are some relevant pieces of code</p>
  <p>(I am trying to ping the GetAll() function in handler.go)</p>
  <p>handler.go</p>
  <pre><code>package Routes
  
  import (
      &quot;context&quot;
      &quot;encoding/json&quot;
      &quot;fmt&quot;
      &quot;log&quot;
  
      &quot;github.com/RashadArbab/goServer/Database&quot;
      &quot;github.com/gofiber/fiber/v2&quot;
      &quot;go.mongodb.org/mongo-driver/bson&quot;
  )
  
  var DB = Database.DB
  var ctx = context.TODO()
  
  func GetAll(c *fiber.Ctx) error {
  
      fmt.Println(&quot;Checkpoint 1&quot;)
      var users []Database.User
  
      
  
      cursor, err := DB.Find(ctx, bson.M{})
      if err != nil {
          log.Fatal(err)
      }
  
      fmt.Println(&quot;Checkpoint 2&quot;)
  
      // 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(&quot;Checkpoint 3&quot;)
  
      for cursor.Next(context.TODO()) {
  
          // create a value into which the single document can be decoded
          var user Database.User
          // &amp; character returns the memory address of the following variable.
          err := cursor.Decode(&amp;user) // decode similar to deserialize process.
          if err != nil {
              log.Fatal(err)
          }
  
          // add item our array
          users = append(users, user)
      }
  
      fmt.Println(&quot;Checkpoint 4&quot;)
  
      documents, _ := json.Marshal(users)
      return c.JSON(documents)
  
  }
  
  func CreateProd(c *fiber.Ctx) error {
      return c.SendString(&quot;This is create product&quot;)
  }
  
  func GetSingle(c *fiber.Ctx) error {
      id := c.Params(&quot;id&quot;)
      return c.SendString(&quot;this is create singular&quot; + id)
  }
  </code></pre>
  <p>main.go</p>
  <pre><code>package main
  
  import (
      &quot;github.com/RashadArbab/goServer/Database&quot;
      &quot;github.com/RashadArbab/goServer/Routes&quot;
  
      &quot;github.com/gofiber/fiber/v2&quot;
  )
  
  func main() {
      Database.Init()
  
      app := fiber.New()
  
      app.Static(&quot;/&quot;, &quot;./AA-Frontend/build&quot;)
  
      app.Get(&quot;/&quot;, func(c *fiber.Ctx) error {
          return c.SendString(&quot;hello there&quot;)
      })
  
      Routes.SetupRoutes(app)
  
      app.Listen(&quot;:5000&quot;)
  }
  </code></pre>
  <p>database.go</p>
  <pre><code>package Database
  
  import (
      &quot;go.mongodb.org/mongo-driver/mongo&quot;
      &quot;go.mongodb.org/mongo-driver/mongo/options&quot;
  
      &quot;context&quot;
      &quot;fmt&quot;
      &quot;log&quot;
  )
  
  var DB *mongo.Collection
  var ctx = context.TODO()
  
  func Init() *mongo.Collection {
      clientOptions := options.Client().ApplyURI(&quot;mongodb://localhost:27017/&quot;)
      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(&quot;GoServer&quot;).Collection(&quot;users&quot;)
      fmt.Print(DB.Name())
  
      return DB
  }
  </code></pre>