Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2589

cursor.All code in the find API usage example leads users to a Go gotcha in the case of no results

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor - P4 Minor - P4
    • 2.0.0
    • None
    • None
    • None

    Description

      The "fully runnable example" from the Find Multiple Documents usage example page declares the results slice that is passed to `cursor.All` as

      var results []bson.M
      

      In the case of no results, `results` will remain in an uninitialized state, which will be encoded as `null` in JSON (and BSON).

      In contrast, if the results slice is declared as

      var results []bson.M = make([]bson.M, 0, 0)
      // or, equivalently
      results := make([]bson.M, 0, 0)
      

      In the case of no results, `results` will be fully initialized, and will be encoded as `[]` (the empty array) in JSON.

      Need to initialize a nil slice in `cursor.All` when there are no results.

      Attachments

        Activity

          People

            Unassigned Unassigned
            qingyang.hu@mongodb.com Qingyang Hu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: