duanmu0834 2019-04-20 21:10
浏览 592
已采纳

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

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条回答 默认 最新

  • douli7841 2019-04-21 02:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?