0

I want to retrieve list of ids (type long) from the mongo collection

ids: = [] int64
if count >= 5 {
  err = collection.Find(query).Select(bson.M {
    "_id": 1
  }).Skip(rand.Intn(count - 4)).Limit(4).All(ids)
}

I get an error stating http: panic serving [::1]:62322: result argument must be a slice address

I tried using make for getting the slice, which resulted in the same error

ids: = make([]int64, 0, 4)
if count >= 5 {
  err = collection.Find(query).Select(bson.M {
    "_id": 1
  }).Skip(rand.Intn(count - 4)).Limit(4).All(ids)
}

1 Answer 1

4

Pass a pointer to the slice to All:

ids: = []int64
if count >= 5 {
  err = collection.Find(query).
      Select(bson.M{"_id": 1}).
      Skip(rand.Intn(count - 4)).
      Limit(4).
      All(&ids)  // <-- change is here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.