Ami
Ami

Reputation: 357

Golang MongoDB Error: result argument must be a slice address - (Arrays)

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)
}

Upvotes: 0

Views: 1564

Answers (1)

Thundercat
Thundercat

Reputation: 120941

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
}

Upvotes: 4

Related Questions