Home > Blockchain >  Golang bson.E not declared by package bson
Golang bson.E not declared by package bson

Time:08-03

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

CodePudding user response:

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.

CodePudding user response:

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

Hope this helps.

CodePudding user response:

According to https://stackoverflow.com/users/13415116/phonaputer

I actually imported the wrong package it was supposed to be

    import (
    
    "go.mongodb.org/mongo-driver/bson"
    
)

but I did

import (
    "gopkg.in/mgo.v2/bson"
)

Which solved the error and it became correct

var updatedObj primitive.D
updatedObj = append(updatedObj, bs.E{"table", order.Table_id})
  • Related