Search code examples
mongodbgogo-modules

How to import the libraries in mongodb gridfs?


I am trying to work on MongoDB gridfs and I am new to it. I found online resources to make the gridfs work in golang and I found this article. When I took the code and tried to run it, it gave me the following error.

Error

could not import go.mongodb.org/mongo-driver/mongo/gridfs (cannot find package "go.mongodb.org/mongo-driver/mongo/gridfs" in any of 
/usr/local/go/src/go.mongodb.org/mongo-driver/mongo/gridfs (from $GOROOT)
/home/bilal/go/src/go.mongodb.org/mongo-driver/mongo/gridfs (from $GOPATH))

Libraries

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"

These four libraries have given me this error. But I gave just one error example.


Solution

  • Seems like you haven't installed the dependencies properly.

    First you need to set up your project with go mod init PROJECT-NAME as described in the referenced article.

    Then you need to install the dependencies as outlined in the article you initially linked:

    go get go.mongodb.org/mongo-driver/mongo
    go get go.mongodb.org/mongo-driver/mongo/gridfs