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

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.0.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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.

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

              Created:
              Updated:
              Resolved: