Mongodb-go-driver接口转换:interface {}是原始的.ObjectID,而不是原始的.ObjectID(来自不同包的类型)

我可以显示result.InsertedID没问题,但是,当我尝试访问时,我仍然收到上述错误 在其他地方使用它的价值。 我想在离开插入新对象的方法之前获取值。 该错误消息没有任何意义。 </ p>

我使用res.InsertedID。(primitive.ObjectID)遇到此错误。 正确的方法是什么?</ p>

如何最好以十六进制字符串的形式获取具体值? 即使我可以将值作为一个具体的ObjectID而不是一个接口来获取,我也会很高兴。</ p>
</ div>

展开原文

原文

I can display the result.InsertedID with no problem, however, I keep getting the above error when I attempt to access the value to use it other places. I want to get the value before I leave the method that inserts the new object. The error message does not make any sense.

I get this error with res.InsertedID.(primitive.ObjectID). What is the proper way?

How do I get the concrete value preferably as a hex string? Even if I can get the value as a concrete ObjectID and not an interface I would be happy.

1个回答



应用程序中有两个名称为 primitive </ code>的包,其中包含名为 ObjectID </ code的类型 >。 类型断言中的具体值来自这些软件包之一。 断言的类型来自另一个包。 类型断言会出现恐慌,因为类型不相等。</ p>

通过修改应用程序以对 primitive </ code>包(和整个驱动程序)使用单个导入路径进行修复 </ p>

该软件包的规范导入路径已从 github.com/mongodb/mongo-go-driver/bson/primitive </ code>更改为 go。 mongodb.org/mongo-driver/bson/primitive </ code>于2019年2月发布。应用程序可能同时使用了这两个导入路径。</ p>
</ div>

展开原文

原文

There are two packages in the application with name primitive containing a type named ObjectID. The concrete value in the type assertion is from one of those packages. The asserted type is from the other package. The type assertion panics because the types are not equal.

Fix by modifying the application to use a single import path for the primitive package (and the entire driver).

The canonical import path of the package was changed from github.com/mongodb/mongo-go-driver/bson/primitive to go.mongodb.org/mongo-driver/bson/primitive in February 2019. It is likely that the application uses both of these import paths.

dousi7919
dousi7919 这应该是可以接受的答案,这确实是上述错误的原因
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐