[GODRIVER-2589] cursor.All code in the find API usage example leads users to a Go gotcha in the case of no results Created: 13/Oct/22  Updated: 20/Oct/23

Status: Backlog
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.0.0

Type: Improvement Priority: Minor - P4
Reporter: Qingyang Hu Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-2553 docs: cursor.All code in the find API... Closed

 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.



 Comments   
Comment by Ramit Mittal [ 20/Oct/23 ]

Do we really need to return `[]` instead of `nil`?
There might be users that depend on this behavior.

Generated at Thu Feb 08 08:38:58 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.