英文:
Golang MongoDB Error: result argument must be a slice address - (Arrays)
问题
我想从Mongo集合中检索ID列表(类型为long)。
ids := []int64
如果count >= 5 {
err = collection.Find(query).Select(bson.M{
"_id": 1
}).Skip(rand.Intn(count - 4)).Limit(4).All(ids)
}
我收到一个错误,错误信息是http: panic serving [::1]:62322: result argument must be a slice address。
我尝试使用make来获取切片,但结果出现了相同的错误。
ids := make([]int64, 0, 4)
如果count >= 5 {
err = collection.Find(query).Select(bson.M{
"_id": 1
}).Skip(rand.Intn(count - 4)).Limit(4).All(ids)
}
英文:
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
得分: 4
将切片的指针传递给All
函数:
ids := []int64
if count >= 5 {
err = collection.Find(query).
Select(bson.M{"_id": 1}).
Skip(rand.Intn(count - 4)).
Limit(4).
All(&ids) // <-- 修改在这里
}
英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论