skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

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

  2. 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})
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search