英文:
Golang bson.E not declared by package bson
问题
目前正在进行一个 Golang 项目,但在一些控制器中遇到了问题:
package controller
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2/bson"
)
var updatedObj primitive.D
updatedObj = append(updatedObj, bson.E{"table", order.Table_id})
我总是得到错误信息:(bson.E) E is not declared by package bson
。
英文:
Currently working on a Golang project but in some controllers i get
package controller
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2/bson"
)
var updatedObj primitive.D
updatedObj = append(updatedObj, bson.E{"table", order.Table_id})
I always get (bson.E) E is not declared by package bson
答案1
得分: 6
看起来你导入了错误的bson
包。
如你所见这里,gopkg.in/mgo.v2/bson
不包含"E"类型。
根据你使用的其他包,我认为你可能想要使用这个包:go.mongodb.org/mongo-driver/bson
。你正在使用的primitive
包是这个包的子包,所以我认为这两个应该可以正确配合使用。
英文:
It looks like you are importing the wrong bson
package.
As you can see here, gopkg.in/mgo.v2/bson
does not include the "E" type.
Based on the other packages you are using, I think perhaps you want this one: go.mongodb.org/mongo-driver/bson
? The primitive
package you are using is a subpackage of this package so I think the two should work together correctly.
答案2
得分: 0
我认为这是因为bson包中没有包含任何E
(因为它正在读取其他包:gopkg.in/mgo.v2/bson
)。
你可以尝试像这样的代码:
package controller
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2/bson"
)
var updatedObj primitive.D
updatedObj = append(updatedObj, primitive.E{"table", order.Table_id})
英文:
I think it is because bson doesn't have any E
included in their package (because it is reading the other package: gopkg.in/mgo.v2/bson
).
You can try something like this:
package controller
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2/bson"
)
var updatedObj primitive.D
updatedObj = append(updatedObj, primitive.E{"table", order.Table_id})
答案3
得分: 0
根据https://stackoverflow.com/users/13415116/phonaputer的说法,
我实际上导入了错误的bson包
import (
"gopkg.in/mgo.v2/bson"
)
这样解决了错误,并且变得正确
import (
"go.mongodb.org/mongo-driver/bson"
)
var updatedObj primitive.D
updatedObj = append(updatedObj, bs.E{"table", order.Table_id})
英文:
According to https://stackoverflow.com/users/13415116/phonaputer
I actually imported the wrong package for bson
import (
"gopkg.in/mgo.v2/bson"
)
This solved the error and it became correct
import (
"go.mongodb.org/mongo-driver/bson"
)
var updatedObj primitive.D
updatedObj = append(updatedObj, bs.E{"table", order.Table_id})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论